C# reach click button in webbrowser?

蹲街弑〆低调 提交于 2019-12-01 12:44:37

问题


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



来源:https://stackoverflow.com/questions/15901997/c-sharp-reach-click-button-in-webbrowser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!