vb.net 没有 ID 或名称的 Web 浏览器单击按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18480731/
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
Webbrowser Click Button With No ID Or Name
提问by user2724175
I am trying to use the WebBrowser in Visual Studio 2012 (Visual Basic) to click a button. I have done a lot of research, but all I can find is to click with an ID or a name.
我正在尝试使用 Visual Studio 2012 (Visual Basic) 中的 WebBrowser 单击按钮。我做了很多研究,但我能找到的只是用一个 ID 或一个名字点击。
Here is the HTML code.
这是 HTML 代码。
<button type='submit' class="btn btn-default" style='float:right'>
Login<i class="gicon-chevron-right"></i>
I am going to have the webbrowser hidden, & then when the user clicks Button1, it invokes the click on the webbrowser. Please help.
我将隐藏 webbrowser,然后当用户单击 Button1 时,它会调用 webbrowser 上的单击。请帮忙。
回答by SysDragon
You need to search for your element one by one. Try something like this:
您需要一一搜索您的元素。尝试这样的事情:
For Each elem As HtmlElement In wb1.Document.GetElementsByTagName("button")
' Check the attributtes you want
If elem.GetAttribute("class") = "btn btn-default" Then
'Check even the text if you want
If elem.InnerText = "Login" Then
'Invoke your event
elem.InvokeMember("click")
End If
End If
Next
This will search on all the buttonelements on the document. You could narrow the searching by looking inside some element that contains your button that has ID or something:
这将搜索button文档上的所有元素。您可以通过查看包含具有 ID 或其他内容的按钮的某些元素来缩小搜索范围:
Dim myElem As HtmlElement = wb1.Document.GetElementById("myContainerId")
For Each elem As HtmlElement In myElem.GetElementsByTagName("button")
'...
Next
回答by Abdullah Kassha
simple code :
简单的代码:
WebBrowser1.Document.Forms(0).InvokeMember("submit")
回答by Lightningbbrains
Dim HtmlElementcoll As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
For Each elem As HtmlElement In HtmlElementcoll
' Check the attributtes you want
If elem.GetAttribute("value") = "Sign In" Then
'Check even the text if you want
' If elem.InnerText = "Sign In" Then
'Invoke your event
elem.InvokeMember("click")
'End If
End If
Next
i have modified the code above and its working add HtmlElementCollection and you are good to go..
我已经修改了上面的代码,它的工作添加了 HtmlElementCollection,你很高兴..

