我正在尝试使用Visual Studio 2012(Visual Basic)中的WebBrowser来单击按钮。我做了很多研究,但我能找到的只是点击ID或名称。
这是HTML代码。
<button type='submit' class="btn btn-default" style='float:right'>
Login<i class="gicon-chevron-right"></i>
我将隐藏web浏览器,&amp;然后当用户单击Button1时,它会调用webbrowser上的单击。请帮忙。
答案 0 :(得分:2)
您需要逐个搜索您的元素。尝试这样的事情:
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
这将搜索文档中的所有button
元素。您可以通过查看包含具有ID或其他内容的按钮的某个元素来缩小搜索范围:
Dim myElem As HtmlElement = wb1.Document.GetElementById("myContainerId")
For Each elem As HtmlElement In myElem.GetElementsByTagName("button")
'...
Next
答案 1 :(得分:2)
简单代码:
WebBrowser1.Document.Forms(0).InvokeMember("submit")
答案 2 :(得分:0)
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
我修改了上面的代码并且它的工作添加了HtmlElementCollection,你很高兴..