How do you click a button in a webbrowser control?

后端 未结 3 1212
遇见更好的自我
遇见更好的自我 2020-11-29 20:03

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 googl

相关标签:
3条回答
  • 2020-11-29 20:29
    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";
    
    0 讨论(0)
  • 2020-11-29 20:30

    In addition to using InvokeMember and others, if your web page has issues responding when called by ID or Class, you can try using {TAB} & {ENTER} using the SendKeys class within .NET. I've written a lot of scripts for web pages and have found that I've had to use a combination of both (even though SendKeys is far messier than the methods in @AleWin's answer).

    Here is the link to the SendKeys class.

    0 讨论(0)
  • 2020-11-29 20:49

    Try the following code:

    public WebBrowser webBrowser1 = new WebBrowser();
        private void WebForm_Load(object sender, EventArgs e)
            {
                try
                {
                    webBrowser1.Height = 1000;
                    webBrowser1.Width = 1000;
                    this.Controls.Add(webBrowser1);
                    webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
                    this.webBrowser1.Navigate("www.google.com.au");
                }
                catch
                { }
    

    Write down the following function in your c# form:

    public void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            var webBrowser = sender as WebBrowser;
            webBrowser.DocumentCompleted -= WebBrowser_DocumentCompleted;
    
            HtmlElement textElement = webBrowser.Document.All.GetElementsByName("q")[0];
            textElement.SetAttribute("value", "mlm company");
            HtmlElement btnElement = webBrowser.Document.All.GetElementsByName("btnG")[0];
            btnElement.InvokeMember("click");
    
    
        }
    
    0 讨论(0)
提交回复
热议问题