Using webbrowser to click on a button in a webpage

两盒软妹~` 提交于 2019-12-09 13:49:39

问题


so what I need to do is to use this code:

WebBrowser browser = new WebBrowser();
browser.Navigate("www.somthing.com");
browser.Document.GetElementById("ID").InvokeMember("click");

and then i need to find the id of the button in the page and put in my code but my problem is some of the buttons don't have Id's! what should I do then? they only have type and class and some other things but no Id. i realized that some buttons might be java and maybe that's why i can't click on them by the usual way. so do you know what should I do?


回答1:


You said your button element has a className ,you can get this element with like this:

public Form1()
        {
            InitializeComponent();
            webBrowser1.Navigate("To your register account url");
        }
        int on = 0;
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            foreach (HtmlElement btn in webBrowser1.Document.GetElementsByTagName("button"))
            {
                if (btn.GetAttribute("className") == "yourclassname")
                {
                    btn.InvokeMember("Click");
                    break;
                }
            }
        }


来源:https://stackoverflow.com/questions/17334909/using-webbrowser-to-click-on-a-button-in-a-webpage

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