1

我正在尝试编写一些代码来在 excel 单元格中查找一个单词,搜索该单词,最重要的是,单击返回的链接。

我有搜索部分,但它是我正在努力的链接的点击。HMTL 提取物是 -

<span>
<div class="searchResult webResult ">
<div class="resultTitlePane">
<h3>
<a class="outbound" href="whatever" target ="" rel="nofollow" rev="lots of 
text">..</a>
</h3>
</div>

我没有输入 href 和 rel 文本,但我只想能够单击返回的链接并继续访问该站点。请问有什么帮助吗?

这是我的代码 -

Sub test()
Dim ie As InternetExplorer
Dim RegEx As RegExp, RegMatch As MatchCollection
Dim MyStr As String
Dim pDisp As Object
Dim dtStartTime As Date
Set ie = New InternetExplorer
Set RegEx = New RegExp
Dim iedoc As Object
SearchEng = "http://easysearch.org.uk/search/?s="

LastRow = Range("A1").End(xlDown).Row

Do Until i = LastRow + 1
    SearchMe = Range("A" & i)
    ie.Navigate SearchEng & SearchMe
    Do Until ie.ReadyState = READYSTATE_COMPLETE
    Loop

    MyStr = ie.document.body.innerText
    Set RegMatch = RegEx.Execute(MyStr)        

    If RegMatch.Count > 0 Then
        ie.Navigate RegMatch(0)
        Do Until ie.ReadyState = READYSTATE_COMPLETE
        Loop

        ie.Visible = True        
        Set iedoc = ie.document
        ''NEED TO ADD SOMETHING HERE TO CLICK LINK''

    End If

    i = i + 1
    Do Until ie.ReadyState = READYSTATE_COMPLETE
    Loop
Loop 
Set RegEx = Nothing
ie.Quit
Set ie = Nothing
End Sub
4

1 回答 1

0

在您的代码中,您可以将 placeholder: 替换''NEED TO ADD SOMETHING HERE TO CLICK LINK''为:

iedoc.getElementsByClassName("resultTitlePane")(0).getElementsByTagName("a")(0).Click

针对OP对 readystate 未完成的评论:

您可以继续使用您的 readystate 循环,然后循环实际对象直到它可用,然后单击它:

Do Until ie.ReadyState = READYSTATE_COMPLETE
Loop

ie.Visible = True        
Set iedoc = ie.document

' ^ ^ ^ ^ Already part of your code

Dim myBtn As Object
Set myBtn = Nothing

' Loop object while it's Nothing
On Error Resume Next
Do While myBtn Is Nothing
    DoEvents
    set myBtn = iedoc.getElementsByClassName("resultTitlePane")(0).getElementsByTagName("a")(0)
Loop
On Error GoTo 0

myBtn.Click
于 2018-03-04T22:48:22.770 回答