Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you click a button in a webbrowser control?

Tags:

browser

c#

button

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)

like image 768
Minicl55 Avatar asked Jun 30 '12 05:06

Minicl55


People also ask

How to use WebBrowser in c#?

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.

What is WebBrowser control?

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.


1 Answers

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";
like image 107
Alessio Koci Avatar answered Oct 14 '22 01:10

Alessio Koci