For example, using code and no user input, how would I have my program click the "Search" button on google (assuming I've already filled in the search box and am at google.com)
webBrowser1.Navigate("http://www.google.com");
If you have an ID use this:
webBrowser1.Document.GetElementById("id").InvokeMember("click");
If you have TagName use this
webBrowser1.Navigate("http://www.google.com");
In Web Browser DocumentCompleted event
HtmlElement textElement = webBrowser1.Document.All.GetElementsByName("q")[0];
textElement.SetAttribute("value", "your text to search");
HtmlElement btnElement = webBrowser1.Document.All.GetElementsByName("btnG")[0];
btnElement.InvokeMember("click");
If you have name Class use this:
HtmlElementCollection classButton = webBrowser1.Document.All;
foreach (HtmlElement element in classButton)
{
if (element.GetAttribute("className") == "button")
{
element.InvokeMember("click");
}
}
For adding text in a TextBox to search google.com, use this:
webBrowser1.Document.GetElementById("gs_tti0").InnerText = "hello world";
Try the following code:
public WebBrowser webBrowser1 = new WebBrowser();
private void WebForm_Load(object sender, EventArgs e)
{
try
{
webBrowser1.Height = 1000;
webBrowser1.Width = 1000;
this.Controls.Add(webBrowser1);
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
this.webBrowser1.Navigate("www.google.com.au");
}
catch
{ }
Write down the following function in your c# form:
public void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var webBrowser = sender as WebBrowser;
webBrowser.DocumentCompleted -= WebBrowser_DocumentCompleted;
HtmlElement textElement = webBrowser.Document.All.GetElementsByName("q")[0];
textElement.SetAttribute("value", "mlm company");
HtmlElement btnElement = webBrowser.Document.All.GetElementsByName("btnG")[0];
btnElement.InvokeMember("click");
}
In addition to using InvokeMember and others, if your web page has issues responding when called by ID or Class, you can try using {TAB} & {ENTER} using the SendKeys class within .NET. I've written a lot of scripts for web pages and have found that I've had to use a combination of both (even though SendKeys is far messier than the methods in #AleWin's answer).
Here is the link to the SendKeys class.
Related
I'm writing a software that gets the content from URL. When working on that, I run into to problem that I can not get exactly the HTML content after the java script finished.
There are some websites that renders HTML by java-script, some do not support browsers which does not run js.
I tried using System.Windows.Controls.WebBrowser with WebBrowser.Document in LoadCompleted but no luck.
After that, I tried the OpenWebkitSharp library. On the UI, it showes the content of website correctly, but with code Document in DocumentCompleted, it still returns the content which does not rendered by java-script.
Here is my code:
...
using WebKit;
using WebKit.Interop;
public MainWindow()
{
windowFormHost = new System.Windows.Forms.Integration.WindowsFormsHost();
webBrowser = new WebKit.WebKitBrowser();
webBrowser.AllowDownloads = false;
windowFormHost.Child = webBrowser;
grdBrowserHost.Children.Add(windowFormHost);
webBrowser.Load += WebBrowser_Load;
}
private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var contentHtml = ((WebKitBrowser)sender).DocumentAsHTMLDocument;
}
The contentHtml has value which is not rendered after java-script finished.
Do solve this problem, I have added some trick into my code to get the full Html content after java-script finished.
using WebKit;
using WebKit.Interop;
using WebKit.JSCore; //We need add refrence JSCore which following with Webkit package.
public MainWindow()
{
InitializeComponent();
InitBrowser();
}
private void InitBrowser()
{
windowFormHost = new System.Windows.Forms.Integration.WindowsFormsHost();
webBrowser = new WebKit.WebKitBrowser();
webBrowser.AllowDownloads = false;
windowFormHost.Child = webBrowser;
grdBrowserHost.Children.Add(windowFormHost);
webBrowser.Load += WebBrowser_Load;
}
private void WebBrowser_Load(object sender, EventArgs e)
{
//The ResourceIntercepter will throws exception if webBrowser have not finished loading its components
//We can not use DocumentCompleted to load the Htmlcontent. Because that event will be fired before Java-script is finised
webBrowser.ResourceIntercepter.ResourceFinishedLoadingEvent += new ResourceFinishedLoadingHandler(ResourceIntercepter_ResourceFinishedLoadingEvent);
}
private void ResourceIntercepter_ResourceFinishedLoadingEvent(object sender, WebKitResourcesEventArgs e)
{
//The WebBrowser.Document still show the html without java-script.
//The trict is call Javascript (I used Jquery) to get the content of HTML
JSValue documentContent = null;
var readyState = webBrowser.GetScriptManager.EvaluateScript("document.readyState");
if (readyState != null && readyState.ToString().Equals("complete"))
{
documentContent = webBrowser.GetScriptManager.EvaluateScript("$('html').html();");
var contentHtml = documentContent.ToString();
}
}
Hope this one can help you.
I have a WebBrowser control:
<phone:WebBrowser Name="ArticleContent" Navigating="ArticleContent_Navigating" Navigated="ArticleContent_Navigated" />
And i get article from server like a HTML string:
string Article = "<p>Sometext</p><span style=\"font-family:"Arial","sans-serif";mso-fareast-font-family:"Arial Unicode MS"; mso-fareast-language:LV\">artjomgsd#inbox.lv</span>";
I do this:
ArticleContent.NavigateToString(Article);
And have this function to stop loading icon:
private void ArticleContent_Navigated(object sender, NavigationEventArgs e)
{
HideLoading();
}
And this function to handle links ( to open links in external browser):
private void ArticleContent_Navigating(object sender, NavigatingEventArgs e)
{
e.Cancel = true;
WebBrowserTask webBrowserTask = new WebBrowserTask();
webBrowserTask.Uri = new Uri(e.Uri.ToString(), UriKind.Absolute);
webBrowserTask.Show();
}
My question is, why when i tap E-mail hyperlink nothing hapens? It even doesn't enter ArticleContent_Navigating() function?
P.S. I want to open MailTask on clicking on mail hyperlink.
Unfortunately, mailto: it not supported by the WebBrowser control on Windows Phone.
What you can do is inject Javascript in the HTML that will enumerate all a tags and wire up an onclick event. That event will call window.external.Notify which will in turn raise the ScriptNotify event of the WebBrowser, with the URL as a parameter.
It is a little complicated but I think it's the only option for dealing with the mailto protocol on Windows Phone.
Here is some sample code:
// Page Constructor
public MainPage()
{
InitializeComponent();
browser.IsScriptEnabled = true;
browser.ScriptNotify += browser_ScriptNotify;
browser.Loaded += browser_Loaded;
}
void browser_Loaded(object sender, RoutedEventArgs e)
{
// Sample HTML code
string html = #"<html><head></head><body><a href='mailto:test#test.com'>Send an email</a></body></html>";
// Script that will call raise the ScriptNotify via window.external.Notify
string notifyJS = #"<script type='text/javascript' language='javascript'>
window.onload = function() {
var links = document.getElementsByTagName('a');
for(var i=0;i<links.length;i++) {
links[i].onclick = function() {
window.external.Notify(this.href);
}
}
}
</script>";
// Inject the Javascript into the head section of the HTML document
html = html.Replace("<head>", string.Format("<head>{0}{1}", Environment.NewLine, notifyJS));
browser.NavigateToString(html);
}
void browser_ScriptNotify(object sender, NotifyEventArgs e)
{
if (!string.IsNullOrEmpty(e.Value))
{
string href = e.Value.ToLower();
if (href.StartsWith("mailto:"))
{
EmailComposeTask email = new EmailComposeTask();
email.To = href.Replace("mailto:", string.Empty);
email.Show();
}
}
}
Problem with your Html tag just write Http// before mailto:artjomgsd#inbox.lv\ on your link it will work fine
like bellow
"<p>Sometext</p><span style=\"font-family:"Arial","sans-serif";mso-fareast-font-family:"Arial Unicode MS"; mso-fareast-language:LV\">artjomgsd#inbox.lv</span>";
i tested this code in my application now its work..
so what I need to do is to use this code:
WebBrowser browser = new WebBrowser();
browser.Navigate("www.somthing.com");
browser.Document.GetElementById("ID").InvokeMember("click");
and then i need to find the id of the button in the page and put in my code but my problem is some of the buttons don't have Id's! what should I do then? they only have type and class and some other things but no Id. i realized that some buttons might be java and maybe that's why i can't click on them by the usual way. so do you know what should I do?
You said your button element has a className ,you can get this element with like this:
public Form1()
{
InitializeComponent();
webBrowser1.Navigate("To your register account url");
}
int on = 0;
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
foreach (HtmlElement btn in webBrowser1.Document.GetElementsByTagName("button"))
{
if (btn.GetAttribute("className") == "yourclassname")
{
btn.InvokeMember("Click");
break;
}
}
}
I want to hide an HTML tag in my web browser.The problem that I have is the fact that I must not use javascript.
I have come up with this block of code:
public void HideHTMLTag(string ControlID)
{
HtmlDocument doc = webBrowser1.Document;
HtmlElement HTMLControl = doc.GetElementById(ControlID);
HTMLControl.Style = "'display: none;'";
webBrowser1.Refresh();
}
I call this in a button event.I appreciate your help.
Please try the code in documentCompleted event..
URL:- Hiding HTML tag in webbrowser
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlDocument doc = webBrowser1.Document;
HtmlElement HTMLControl = doc.GetElementById("question-header");
//HTMLControl.Style = "'display: none;'";
if (HTMLControl != null)
{
HTMLControl.Style = "display: none";
}
}
Before I go off and code this, I thought I'd see if anyone here knows of any open source (or paid for) equivalents already built.
I'm looking for a browser control where users can preview a web page and then highlight elements of it and once highlighted, I can get the div or id of the element selected.
Has anyone seen such a thing?
Here's a crude version using the .NET WebBrowser control, which uses Internet Explorer.
namespace WindowsFormsApplication1
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
public partial class Form1 : System.Windows.Forms.Form
{
private HtmlDocument document;
private IDictionary<HtmlElement, string> elementStyles = new Dictionary<HtmlElement, string>();
public Form1()
{
InitializeComponent();
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
this.Text = e.Url.ToString();
this.document = this.webBrowser1.Document;
this.document.MouseOver += new HtmlElementEventHandler(document_MouseOver);
this.document.MouseLeave += new HtmlElementEventHandler(document_MouseLeave);
}
private void document_MouseLeave(object sender, HtmlElementEventArgs e)
{
HtmlElement element = e.FromElement;
if (this.elementStyles.ContainsKey(element))
{
string style = this.elementStyles[element];
this.elementStyles.Remove(element);
element.Style = style;
}
}
private void document_MouseOver(object sender, HtmlElementEventArgs e)
{
HtmlElement element = e.ToElement;
if (!this.elementStyles.ContainsKey(element))
{
string style = element.Style;
this.elementStyles.Add(element, style);
element.Style = style + "; background-color: #ffc;";
this.Text = element.Id ?? "(no id)";
}
}
}
}
I had a similar need and the code originally posted was a tremendous help. I would like to return the kindness of the original author.
The answer provided is insufficient. It only answers the first part of the request: highlighting the rendered HTML. It does not solve the second half of the request where the C# program can tell which node is selected.
As the mouse enters the form and moves towards the desired rendered area, the highlight will follow the mouse. This means that potentially many HTML elements will be visited by the mouse.
So how can the C# tell which element the user wants?
The program has to allow the user to click on the desired rendered area. The click should be trapped by the C# program. In addition, the C# should disable the click being delivered to the element (for example, if its an Anchor element you don't want the click to follow the link).
I solved this in my program as follows:
(1) On the 'document complete' event I added a trap (only once for the document) for the click event at the document level:
HtmlDocument htmlDoc = webBrowser.Document;
_document = webBrowser.Document;
//-- add our handler only once
if (!_documentHandlers.Contains(_document))
{
_document.MouseOver += new HtmlElementEventHandler(document_MouseOver);
_document.MouseLeave += new HtmlElementEventHandler(document_MouseLeave);
mshtml.HTMLDocumentEvents2_Event iEvent;
IHTMLDocument2 currentDoc = (IHTMLDocument2) webBrowser.Document.DomDocument;
iEvent = (mshtml.HTMLDocumentEvents2_Event) currentDoc;
iEvent.onclick += new mshtml.HTMLDocumentEvents2_onclickEventHandler(clickDocumentHandler);
_documentHandlers.Add(_document);
}
The 'document completed' event can be triggered several times for the same document (try CNN.com). The '_documentHandlers' variable is a HashSet to make sure we only add the handler one time per a given document.
(2) I also chose to trap the click at the element level. On the 'mouse over' I added:
mshtml.HTMLElementEvents2_Event iEvent;
iEvent = element.DomElement as mshtml.HTMLElementEvents2_Event;
if (iEvent == null)
return;
iEvent.onclick += new mshtml.HTMLElementEvents2_onclickEventHandler(clickElementHandler);
and at the 'mouse leave' I unregistered the click handler:
mshtml.HTMLElementEvents2_Event iEvent;
iEvent = element.DomElement as mshtml.HTMLElementEvents2_Event;
if (iEvent == null)
return;
iEvent.onclick -= new mshtml.HTMLElementEvents2_onclickEventHandler(clickElementHandler);
(3) the click handlers are simple:
private bool clickDocumentHandler(IHTMLEventObj pEvtObj)
{
IHTMLElement element = (mshtml.IHTMLElement)pEvtObj.srcElement;
pEvtObj.cancelBubble = true;
pEvtObj.returnValue = false;
return false;
}
private bool clickElementHandler(IHTMLEventObj pEvtObj)
{
IHTMLElement element = (mshtml.IHTMLElement)pEvtObj.srcElement;
pEvtObj.cancelBubble = true;
pEvtObj.returnValue = false;
return false;
}
Note that they cancel the bubbling of event and return a value of 'false' to prevent the click from percolating upwards.
In these handlers you can add your specific code to save the element being clicked and then use it elsewhere in your application.
Note that at the element level, not every IHTMLElement supports an onclick handler hence the check for null.
-Enjoy
David