本文介绍了如何单击 Webbrowser 控件中的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,使用代码并且没有用户输入,我将如何让我的程序点击 google 上的搜索"按钮(假设我已经填写了搜索框并且在 google.com)
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)
推荐答案
webBrowser1.Navigate("http://www.google.com");
如果您有 ID
,请使用:
If you have an ID
use this:
webBrowser1.Document.GetElementById("id").InvokeMember("click");
如果你有 TagName
使用这个
If you have TagName
use this
webBrowser1.Navigate("http://www.google.com");
在 Web 浏览器 DocumentCompleted 事件中
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");
如果你有名字 Class
使用这个:
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");
}
}
要在 TextBox
中添加文本以搜索 google.com,请使用:
For adding text in a TextBox
to search google.com, use this:
webBrowser1.Document.GetElementById("gs_tti0").InnerText = "hello world";
这篇关于如何单击 Webbrowser 控件中的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!