Home > OS >  Getting HTML link using Internet explorer with VBA excel (no code error but the results is wrong)
Getting HTML link using Internet explorer with VBA excel (no code error but the results is wrong)

Time:11-30

Automating an Internet explorer process by using VBA excel. I am trying to get the first link in this classname 'datacell' with the th scope=row in html using the first 'a href' tag as my needed link . however the result im getting is the wrong link. Could anyone help me about this? There is no error in the code but only the results im getting

refer to the image link - Correct link is the black circled one but the result is red circled one

enter image description here

This is my VBA code:

         Set appIE = New InternetExplorerMedium
           With appIE
          .Visible = False
          .Visible = True
                 Set doc = appIE.document
                    For Each li In doc.getElementsByClassName("list")
                    
                    checkIfAttachment = li.innerHTML
                    
                    If InStr(checkIfAttachment, "No matches found") Then
                        ws.Cells(i, 6).Value = "Not in SFDC"
                    
                    Else
                    
                        Set childLi = Nothing
                        For Each childLi In doc.getElementsByClassName(" dataCell  ")
                            link = childLi.getElementsByTagName("a")(0)
                            'first link is the one we want
                            
                        Next childLi
                        
                        link = Replace(link, "https://sample.com", "")
                        link = Replace(link, "?srPos=0&srKp=00P", "")
                        .navigate baseSFDCDownloadLink & link
                        Do While .Busy = True Or .readyState <> 4: DoEvents: Loop
                        Application.Wait (Now   TimeValue("0:00:05"))

CodePudding user response:

If you are getting a td rather than th element then specify th e.g. with type css selector (adding class as well)

.document.querySelector("th.dataCell").href

CodePudding user response:

you don't need a loop for "datacell". I'm not familiar with VBA but something like this could work .

link = doc.getElementsByClassName(" dataCell ")(0).getElementsByTagName("a")(0)
  • Related