1

I am trying to wrote some code that will lookup a word in an excel cell, the search for that word and, most importantly, click on the link that returns.

I have the search part down, but it's the click on the link I'm struggling with. the HMTL extract is -

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

I've not typed out the href and rel text, but I just want to be able to click the link that's returned and follow through to that site. Any help please?

this is my code -

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
K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43
Simon B
  • 11
  • 2

1 Answers1

0

In your code, you can replace you placeholder: ''NEED TO ADD SOMETHING HERE TO CLICK LINK'' with:

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

In response to OP's comment of readystate not completing:

You can continue to use your readystate loop, and then loop the actual object until it becomes available, then click it:

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
K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43
  • Hi, thanks, but it doesn't look like it's working, it will cycle through all the keywords, but not actually follow through on the link – Simon B Mar 06 '18 at 20:19
  • If I F8 through the code with a stop at this line it works, but not otherwise, does it need some kind of pause there? I tried the code Do Until ie.ReadyState = READYSTATE_COMPLETE Loop but that doesn't seem to work either – Simon B Mar 06 '18 at 20:31
  • If your readystate doesn't work then you can loop the object till it becomes ready. I will update in a second – K.Dᴀᴠɪs Mar 06 '18 at 20:33
  • Hi, thanks, but again I have a problem, it runs once, (but doesn't seem to open the link) then when it gets to the second time around it's erroring at myBtn.Click – Simon B Mar 06 '18 at 20:48
  • does it still work when you step through it manually? Can you add `Msgbox ieDoc is Nothing` right above the loop and tell me if it's true or false? – K.Dᴀᴠɪs Mar 06 '18 at 20:51
  • says FALSE both times – Simon B Mar 06 '18 at 21:02
  • That's good. But does this still run when you F8 it line by line? – K.Dᴀᴠɪs Mar 06 '18 at 21:06
  • it will only do it once if I place a stop on that line, will still error second tine around – Simon B Mar 06 '18 at 22:02
  • @SimonB Oh! I failed to see this was in a loop. We need to set the myBtn to Nothing. Update coming in a second – K.Dᴀᴠɪs Mar 06 '18 at 22:27
  • that seems to work when I step through, but not when running it properly, it still seems to miss the bit where it opens the linked page – Simon B Mar 07 '18 at 16:21
  • Hi @KDavis any chance you could take another quick look at this please? – Simon B Mar 14 '18 at 17:28