简体   繁体   中英

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

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

在此处输入图像描述

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"))

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

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM