三个选项:
- 直接执行javascript函数本身
- 使用 InnerHTML 搜索所有 A 元素
- 使用 ClassName 搜索所有 A 元素
直接执行javascript函数本身
编辑:感谢 pquest 指出您可以直接执行 javascript 函数:
Browser.InvokeScript("openFbLWin_407311");
更复杂(并且不是真的必要):
WbBrowser.Navigate( new Uri("javascript:(function(){ openFbLWin_407311(); })();") )
使用 InnerHTML 搜索所有 A 元素
查看以下问题:单击 WebBrowser 控件中的 HTML 链接
在您的情况下,基于该链接,如下所示:
Dim links As HtmlElementCollection
links = WebBrowser1.Document.GetElementsByTagName("A")
For Each link As HtmlElement In links
If link.InnerHtml.IndexOf("Like") <> -1 Then
link.InvokeMember("Click")
End If
Next
使用 ClassName 搜索所有 A 元素
If link.GetAttribute("className") = "single_like_button btn3-wrap" Then
link.InvokeMember("Click")
End If
所有三种方法:
VB.net
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WebBrowser1.Url = New Uri("file:///D:/clickJSbutton.html")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Method 1: Execute javascript directly
Browser.InvokeScript("openFbLWin_407311");
'WebBrowser1.Navigate(New Uri("javascript:(function(){ openFbLWin_407311(); })();"))
'Method 2: Find the link from all links
Dim links As HtmlElementCollection
links = WebBrowser1.Document.GetElementsByTagName("A")
For Each link As HtmlElement In links
'Method 2B: Using some kind of inner html
If link.InnerHtml.IndexOf("Like") <> -1 Then
link.InvokeMember("Click")
End If
'Method 2C: Using className as identifier
If link.GetAttribute("className") = "single_like_button btn3-wrap" Then
link.InvokeMember("Click")
End If
Next
End Sub
End Class