C# reach click button in webbrowser?

2019-02-21 02:50发布

问题:

I want to click event button in webbrowser object.

Button is clicked I want to reach out and write the code? For example, the login button is clicked on the webbrowser object in the pop-up on the site to capture the event and would like to write the code?

I used Google translate, I'm sorry Is there anything wrong :)

回答1:

I write an example for you(how get event clicked button):

private void Form1_Load(object sender, EventArgs e)
{
//Or navigate to your url                
webBrowser1.DocumentText = "<html><body><button id=\"btn1\" type=\"button\">Click Me!</button><button id=\"btn2\" type=\"button\">Click Me!</button></body></html>";
}

Call Click event:(when page loaded)

 private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {

     webBrowser1.Document.Click += new HtmlElementEventHandler(Document_Click);
    }

Get Active Element When User click on document

 void Document_Click(object sender, HtmlElementEventArgs e)
        {
           //Check Element is Button 
           if (webBrowser1.Document.ActiveElement.TagName == "BUTTON")
           {
             if (webBrowser1.Document.ActiveElement.Id== "your button id")
             {
               //Do someting
             }
           }
        }


回答2:

If I understand correctly, you're using the WebBrowser control in C# WinForms and you would like to fire an event when a button is clicked.

The control doesn't have a click event, but the Document does. To do this you can use WebBrowser.Document.Click event and then loop through the HTML elements to find the one that was clicked.

web.Document.Click += new HtmlElementEventHandler(Document_Click);

This link will help also: webbrowser-control-get-element-by-type