C# 如何单击 Webbrowser 控件中的按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11271737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:12:57  来源:igfitidea点击:

How do you click a button in a webbrowser control?

c#buttonbrowser

提问by Minicl55

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 google.com)

例如,使用代码并且没有用户输入,我如何让我的程序点击 google 上的“搜索”按钮(假设我已经填写了搜索框并且在 google.com)

采纳答案by Alessio Koci

webBrowser1.Navigate("http://www.google.com");

If you have an IDuse this:

如果你有这样的ID用途:

webBrowser1.Document.GetElementById("id").InvokeMember("click");

If you have TagNameuse this

如果你有TagName使用这个

 webBrowser1.Navigate("http://www.google.com");

In Web Browser DocumentCompleted event

在 Web 浏览器 DocumentCompleted 事件中

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 Classuse this:

如果你有名字,请Class使用这个:

HtmlElementCollection classButton = webBrowser1.Document.All;
foreach (HtmlElement element in classButton) 
{
    if (element.GetAttribute("className") == "button")
    {
        element.InvokeMember("click");
    }
}

For adding text in a TextBoxto search google.com, use this:

要在 a 中添加文本TextBox以搜索 google.com,请使用以下命令:

 webBrowser1.Document.GetElementById("gs_tti0").InnerText = "hello world";

回答by user2499974

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:

在你的 C# 表单中写下以下函数:

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");


    }

回答by Ben Holland

In addition to using InvokeMemberand others, if your web page has issues responding when called by IDor Class, you can try using {TAB}& {ENTER}using the SendKeysclass 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 SendKeysis far messier than the methods in @AleWin's answer).

除了使用InvokeMember和其他之外,如果您的网页在被ID或调用时出现响应问题Class,您可以尝试在 .NET 中使用{TAB}&{ENTER}使用SendKeys该类。我已经为网页编写了很多脚本,并且发现我不得不结合使用两者(尽管SendKeys比@AleWin 的答案中的方法要混乱得多)。

Here is the link to the SendKeys class.

这是 SendKeys 类的链接。