使 WebBrowser 在 VB.net 中单击具有特定类 ONCE 的网页上的按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19463854/
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
Make WebBrowser click a button on a webpage with a specific class ONCE in VB.net
提问by Sarim Abbas
I'm trying to make the webbrowser click on a specific button within a webpage:
我试图让网络浏览器点击网页中的特定按钮:
The html code for the button is something like <a class="btn btn-large play">and the code I have so far to click this button is:
该按钮的 html 代码类似于<a class="btn btn-large play">,到目前为止我单击该按钮的代码是:
For Each Element As HtmlElement In WebBrowser2.Document.GetElementsByTagName("a")
If Element.OuterHtml.Contains("btn btn-large play") Then
Element.InvokeMember("click")
End If
This works, but it makes the Webbrowser click the button again and again. Any idea how I can only make it do so twice?
这有效,但它会使 Webbrowser 一次又一次地单击该按钮。知道我怎么只能让它这样做两次吗?
回答by VladL
maybe simply like this? :)
也许就是这样?:)
For Each Element As HtmlElement In WebBrowser2.Document.GetElementsByTagName("a")
If Element.OuterHtml.Contains("btn btn-large play") Then
Element.InvokeMember("click")
Element.InvokeMember("click")
return
End If
回答by Seazoux
And Why do you not try to detect the kind of element in the webbrowser:
以及为什么不尝试检测 webbrowser 中的元素类型:
Code snippet originally of @ElektroStudios, I'm just a lammer who pastes code without attribution.
最初来自@ElektroStudios 的代码片段,我只是一个不注明出处粘贴代码的懒汉。
Dim Document As HtmlDocument
Private Sub WebBrowser_DocumentCompleted(ByVal sender As System.Object, ByVal e As WebBrowserDocumentCompletedEventArgs) _
Handles WebBrowser1.DocumentCompleted
Document = sender.Document
AddHandler document.Click, New HtmlElementEventHandler(AddressOf Document_Click)
End Sub
Private Sub Document_Click(ByVal sender As Object, ByVal e As HtmlElementEventArgs)
Select Case Document.ActiveElement.TagName.ToLower
Case "button" : MsgBox("You've clicked a button")
Case "input" : MsgBox("You've clicked a input")
Case "a" : MsgBox("You've clicked a link")
Case Else
End Select
End Sub
Later you can replace MsgBox("You've clicked a link")by some function or event or sub and do what do you want.. :D
稍后你可以用MsgBox("You've clicked a link")一些函数或事件或子替换并做你想做的事.. :D
回答by Alejandro
Try this:
尝试这个:
Public count as integer=0
Private Sub WebBrowser_DocumentCompleted(ByVal sender As System.Object,ByVal e As WebBrowserDocumentCompletedEventArgs) _
Handles WebBrowser1.DocumentCompleted
If count<2 then
For Each Element As HtmlElement In WebBrowser2.Document.GetElementsByTagName("a")
If Element.OuterHtml.Contains("btn btn-large play") Then
Element.InvokeMember("click")
End If
count=count+1
end if
End Sub
回答by ElektroStudios
Would be sufficient to exit the FORloop when you've already clicked once the item, so try this:
FOR当您已经单击该项目时就足以退出循环,因此请尝试以下操作:
For Each Element As HtmlElement In WebBrowser2.Document.GetElementsByTagName("a")
If Element.OuterHtml.Contains("btn btn-large play") Then
Element.InvokeMember("click")
Exit For
End If
Next Element

