1

I'm working on a Macro to login to a website then auto fill in info I need to register 100s of contracts. I got the macro to pull up the website and login, but once in the website I'm having trouble getting it to click on a link in the site. I'm guessing I can't get the element by ID or tagname, what's the best way to get the element for the link? There is some java on the site, I don't know if it's having trouble interacting with that. I'm pretty new to VBA so I'm a little lost. My code so far and a screen shot of the Inspect element are below. I'm trying to click on a link that says "Vehicle Protection Center; TIA

Sub AUTOFILL()
Dim IE As Object
Dim doc As HTMLDocument

Set IE = CreateObject("InternetExplorer.Application")

IE.Visible = True
IE.Navigate "Ally Auto Dealer Services | Financing, Training, Rewards & More | Ally Auto"

Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop

Set doc = IE.document

IE.document.getElementById("user").Value = "USERNAME"
IE.document.getElementById("password").Value = "PASSWORD"
IE.document.getElementById("processLogin").Click
IE.document.getElementbyTagName("Vehicle Protection Center").Click

End Sub

Here's the html code from the site:

<a href="/ui/dashboard/dealer-apps/VPC" 
  data-track-name="Vehicle Protection Center" 
  data-track-trigger="internal" 
  data-track-elem="link" _ngcontent-c17="">Vehicle Proctection Center</a>
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
LCarn
  • 11
  • 3
  • I think you might have forgot to add the screen print. It will help to identify which element you are trying to click – Zac Jun 10 '20 at 15:19
  • 1
    `getElementbyTagName("Vehicle Protection Center")` there is no element with that tag name and it's `getElementsbyTagName()` which returns a collection, not a single item (though the collection may only have one member) – Tim Williams Jun 10 '20 at 15:53
  • You can use `getElementsbyTagName("a")` and loop over the returned collection until you find the element with innerText "Vehicle Protection Center". Also worth checking you're not failing to wait for a refesh after clicking the "processLogin" button. – Tim Williams Jun 10 '20 at 16:21
  • I don't think it's failing to wait for a refresh, but I'm not sure tbh. – LCarn Jun 10 '20 at 16:51
  • ie.document.querySelector("[data-track-name='Vehicle Protection Center']").click – QHarr Jun 10 '20 at 18:59
  • I'm sorry, at a loss, what would that loop look like? I'm trying to teach myself vba as I'm doing this at work and I don't have any html experience. :-/ So I'll put a line for IE.document.getElementsByTagName("a") then the next line should be a for loop? – LCarn Jun 10 '20 at 19:04

1 Answers1

0

If we use the getElementsbyTagName() method to access the HTML elements, it will get all elements in the document with the specified tag name. After that, we could use For Each statement to loop through the elements and check the InnerText value. Then, based on the value to click the link. Code as below:

    For Each link In IE.document.getElementsbyTagName("a")

        With link
            If .innerText = "Vehicle Proctection Center" Then
            link.Click
            End If
        End With

    Next

Besides, we could also use the querySelector() method, and find the <a> tag based on the href attribute value.

    IE.document.querySelector("a[href='/ui/dashboard/dealer-apps/VPC']").Click

[Note] The querySelector() method returns the first Element within the document that matches the specified selector, or group of selectors. Please check website resource, if there has multiple <a> tags with the same href attribute value, you could use the querySelectorAll() method to find the elements, and then loop through the result.

Edit:

Whole sample code (change the website url to your owns):

Sub Test()
    Dim IE As Object
  
    Set IE = CreateObject("InternetExplorer.Application")
    With IE
        .Visible = True
        .Navigate "<website url>"

        While IE.ReadyState <> 4
            DoEvents
        Wend
        
        For Each link In IE.document.getElementsbyTagName("a")

            With link
                If .innerText = "Vehicle Proctection Center" Then
                link.Click
                End If
            End With

        Next
        
        'IE.document.querySelector("a[href='/ui/dashboard/dealer-apps/VPC']").Click
  
    End With
    Set IE = Nothing
End Sub

Html resource:

<!DOCTYPE html>
<html class="viewer" lang="en">
<head>
    <title></title>
</head>    
<body> 
    <a href="/ui/dashboard/dealer-apps/VPC" 
  data-track-name="Vehicle Protection Center" 
  data-track-trigger="internal" 
  data-track-elem="link" _ngcontent-c17="" onclick="alert('Hello')">Vehicle Proctection Center</a>
</body>
</html>

The screenshot: enter image description here

Community
  • 1
  • 1
Zhi Lv
  • 18,845
  • 1
  • 19
  • 30