1

我试图让网络浏览器点击网页中的特定按钮:

该按钮的 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

这可行,但它使 Webbrowser 一次又一次地单击该按钮。知道我怎么只能让它这样做两次吗?

4

4 回答 4

2

也许只是这样?:)

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
于 2013-10-19T08:14:09.747 回答
1

为什么不尝试检测网络浏览器中的元素类型:

代码片段最初来自@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

稍后你可以用MsgBox("You've clicked a link")一些函数或事件或子替换并做你想做的事..:D

于 2013-10-19T08:21:12.220 回答
1

尝试这个:

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
于 2016-10-21T19:33:21.670 回答
0

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 
于 2013-10-19T09:47:01.083 回答