1,502
社区成员
发帖
与我相关
我的任务
分享
Private frmDenied As Long
Private Sub Command2_Click()
Dim wbDoc As HTMLDocument
Dim blnClicked As Boolean, MyId as String
MyId = "A_ID_702"
frmDenied = 0
Set wbDoc = WebBrowser1.Document
If (Not wbDoc Is Nothing) And WebBrowser1.LocationURL <> "about:blank" Then
blnClicked = ClickTheId(wbDoc, MyId)
If blnClicked Then
MsgBox "The element was clicked.", , "Result"
Else
MsgBox "The element with the specified ID was not found." & _
IIf(frmDenied, "This could be caused by denied access." & _
vbCrLf & "Access to " & frmDenied & " frames was denied!", ""), vbExclamation, "Result"
End If
End If
End Sub
Function ClickTheId(TheDocument As HTMLDocument, TheId As String) As Boolean
Dim ElWithId As Object, oFrame As IHTMLWindow2, oDoc As HTMLDocument, i As Long, bClicked As Boolean
Set ElWithId = TheDocument.getElementById(TheId)
If Not ElWithId Is Nothing Then
On Error Resume Next
ElWithId.focus
On Error GoTo 0
ElWithId.Click
Set ElWithId = Nothing
ClickTheId = True
ElseIf TheDocument.frames.length > 0 Then
For i = 0 To TheDocument.frames.length - 1
Set oFrame = TheDocument.frames(i)
On Error Resume Next
Set oDoc = oFrame.Document
If Err.Number = 70 Then
frmDenied = frmDenied + 1
MsgBox "Error # " & Err.Number & ", " & Err.Description & vbCrLf & _
"Source: " & Err.Source, , "Error"
Else
bClicked = ClickTheId(oDoc, TheId)
If bClicked Then ClickTheId = True: Exit Function
End If
Next i
ClickTheId = False
Else
ClickTheId = False
End If
End Function
<A class=cn_12px_white id=A_ID_702 onclick="javascript:FUNC_MENUBAR_MENU_CLICK('702',childmenu,'','',"
);return false;"href="#">
"> <center>申报 <center> </A>
<A class=cn_12px_white id=A_ID_702
而在这个例子中,第二个元素(下标为1)就是
"javascript:FUNC_MENUBAR_MENU_CLICK('702',childmenu,'','',"
);return false;"href="#">
"> <center>申报 <center> </A>
document.writeln(" <a id=\"A_ID_702\" class="+aclass+"href=\"#\'onclick=\"javascript:FUNC_MENUBAR_MENU_CLICK('702','childmenu','','');
return false;\"> <center>申报 <center> <a>');
Dim html As String, link As String
html = 源代码
link = Split(Split(html, "onclick=")(1), """")(1)
Webbrowser1.Navigate link