0

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.

Correct link is black circled but the result is the red circled one
enter image description here

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"))
5
  • 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)" Commented Nov 28, 2021 at 21:30
  • 1
    Please don't post pictures of html. You can use edit and its associated toolbar to find the icon <> which will allow you to insert and format html. Commented Nov 28, 2021 at 22:28
  • @pfndesign thank you so much! this worked out for me. :) Commented Nov 29, 2021 at 6:42
  • 1
    @QHarr hello thank you for the reminder. this is my first time to try the site anyway. Commented Nov 29, 2021 at 6:43
  • @itspapap I'm glad it's worked for you. i posted the answer Commented Nov 29, 2021 at 8:49

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.