Clicking on a link in a website using VBA

LCarney

New Member
Joined
Feb 10, 2020
Messages
24
Office Version
  1. 365
Platform
  1. Windows
So 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? 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

1591715965348.png
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Try this:
VBA Code:
    Dim VPClink As HTMLAnchorElement, i As Long
    
    Set VPClink = Nothing
    i = 0
    While i < doc.Links.Length And VPClink Is Nothing
        If doc.Links(i).innerText = "Vehicle Protection Center" Then Set VPClink = doc.Links(i)
        i = i + 1
    Wend
    If Not VPClink Is Nothing Then
        VPClink.Click
    Else
        MsgBox "VPC link not found"
    End If
 
Upvote 0
"VPC Link not found" I'm just trying to click through the this link....I'm still learning VBA, I know I'm missing something....
1591723905795.png
 
Upvote 0
Another approach:
VBA Code:
    Dim VPClink As HTMLAnchorElement, i As Long
    Dim Atags As IHTMLElementCollection
    
    Set Atags = doc.getElementsByTagName("A")
    Set VPClink = Nothing
    i = 0
    While i < Atags.length And VPClink Is Nothing
        If InStr(1, Atags(i).innerText, "Vehicle Protection Center", vbTextCompare) Then Set VPClink = Atags(i)
        i = i + 1
    Wend
    If Not VPClink Is Nothing Then
        VPClink.Click
    Else
        MsgBox "VPC link not found"
    End If
If it still doesn't find the VPC link, check if the page contains frames and if so you'll need to access the frame document.
 
Upvote 0
Oh, I think that's it...it does have frames. How do I access that? do I need to define the frame as an object? (I'm really new to VBA and even more new to HTML)
 
Upvote 0
I can't give specific help without access to the page.

First confirm that page uses frames by looking at the HTML source in your browser. You should see frameset or iframe tags. If so, try one of these:

VBA Code:
    Dim iframe As HTMLIFrame
    Set iframe = doc.getElementById("id of frame")  'if the frame has an id
    Set iframe = doc.getElementsByName("name of frame")(0)  'if the frame has a name and is the first such name
    Set doc = iframe.contentDocument

VBA Code:
    Dim frameWindow As HTMLWindow2
    Set frameWindow = doc.frames(0) 'first frame
    Set doc = frameWindow.Document

And then continue with my previous code.
 
Upvote 0
Ok, maybe I'm wrong, I saw an Ifram reference on another page of the site, but looking at it again I don't see it here. what's weird is it'll give me an error now when I run it(object required), but I if wait and resume the code it WILL click through just not without pausing for the error message...
1592234774480.png


This is the code i had updated it to before your initial reply;
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 = "CarnLucy"
IE.document.getElementById("password").Value = "Ally12345"
IE.document.getElementById("processLogin").Click

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

IE.document.getElementsByTagName ("a")
IE.document.querySelector("[data-track-name='Vehicle Protection Center']").Click
 
Upvote 0
what's weird is it'll give me an error now when I run it(object required), but I if wait and resume the code it WILL click through just not without pausing for the error message...
That usually means the element you're trying to reference doesn't exist yet or isn't ready yet. The standard way of dealing with this is a loop which waits until your object variable referencing the element is defined:
VBA Code:
    Dim VPClink As HTMLAnchorElement
    Set VPClink = Nothing
    Do
        Set VPClink = doc.querySelector("[data-track-name='Vehicle Protection Center']")
        DoEvents
    Loop While VPClink Is Nothing
    VPClink.Click
 
Upvote 0
Hey! that totally worked! and it makes sense! thanks so much!
 
Upvote 0

Forum statistics

Threads
1,215,003
Messages
6,122,655
Members
449,091
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top