1

Does anybody know a good way to perform a click on a control inside of a webbrowser? Preferably from ID?

Thanks, ALL suggestions are appreciated.

Freesnöw
  • 30,619
  • 30
  • 89
  • 138

3 Answers3

3

Use the HtmlElement.InvokeMember("click") method. Here's a sample form that uses the Google "I feel lucky" button:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        webBrowser1.Url = new Uri("http://google.com");
        webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
    }

    void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
        if (webBrowser1.Url.Host.EndsWith("google.com")) {
            HtmlDocument doc = webBrowser1.Document;
            HtmlElement ask = doc.All["q"];
            HtmlElement lucky = doc.All["btnI"];
            ask.InnerText = "stackoverflow";
            lucky.InvokeMember("click");
        }
    }
}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • This works but I needed to add this `&& e.Url.ToString()!="about:blank"` to DocumentCompleted if statement – Chuck D Aug 27 '12 at 00:46
0

You can't directly call click on something in the control. However, you can register some JavaScript into the control and perform the click that way.

See How to inject Javascript in WebBrowser control? for details.

Community
  • 1
  • 1
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
0

This isn't really for an ID but you can do the same thing with it by using the value button.

Dim allelements As HtmlElementCollection = WebBrowser1.Document.All

For Each webpageelement As HtmlElement In allelements

If webpageelement.GetAttribute("value") = "Log In" Then

webpageelement.InvokeMember("click")

End If

Next
Chris Wheelous
  • 75
  • 1
  • 1
  • 12