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)
We need to add a web browser control to our form window. Go to [ ToolBox > Common Controls > WebBrowser ], now drag & drop the web browser control to the form, like below. Now one more thing we need to add into the form is statusStrip . This will show the page load progress while user put URLs and hit the Go button.
The WebBrowser control provides a managed wrapper for the WebBrowser ActiveX control. The managed wrapper lets you display Web pages in your Windows Forms client applications.
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";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With