我已经创建了Web浏览器,现在我想通过单击表单上的button1单击Web上的按钮.
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Document.GetElementById("search").SetAttribute("value", IP.Text);
webBrowser1.Document.GetElementById("").InvokeMember("click");
}
Run Code Online (Sandbox Code Playgroud)
但我无法从此字符串中获取按钮ID
<input type="submit" value="?" class="button">
Run Code Online (Sandbox Code Playgroud)
有没有办法解决这个问题?
小智 6
尝试迭代<input>网页中的所有元素.当您找到具有所需值的值时,可以单击该元素.
foreach (HtmlElement el in webBrowser1.Document.GetElementsByTagName("input")
{
if (el.GetAttribute("value").Equals("?"))
{
el.InvokeMember("click");
}
}
Run Code Online (Sandbox Code Playgroud)