How to click hyperlink on webpage

da bobsta

New Member
Joined
Jul 12, 2019
Messages
13
Hi,

I have managed to get on to a webpage through some basic script. On that webpage there are a number of Hyperlinks. I need to identify the appropriate Hyperlink (in the below case it would be where the 'label' is "S-C-BSS-1" and then click it. Is this doable? If so...how?

Element details:

S-C-BSS-1

This is the first time I have ever posted a query so please let me know if you need more information.

Thanks, Da Bobsta


EDIT:
Sorry...the Hyperlink turned into the 'label' when I posted it. Here it is again...


< a href="channel.ssobto?dse_sessionId=zu8R7mHA75xtF3trpz-hKdE&dse_operationName=MyAccounts&dse_applicationName=ALP_EBAN_MyAccounts&dse_threadId=defaultExecutionThreadIdentifier& dse_pageId=0&dse_processorState=myAccounts&dse_processorId=6B0F3D6D2488DA8BE60BAB3A&dse_cmd=continue&dse_nextEventName=a ccountLink&indexAccount=0">S-C-BSS-1
 
Last edited by a moderator:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try adding this to your code. It uses HTML library data types, so you must set a reference to MS HTML Object Library, via Tools -> References in the VBA editor.
Code:
    Dim HTMLdoc As HTMLDocument
    Dim link As HTMLAnchorElement
    Dim i As Long
    
    'After IE navigation and page has loaded
    
    Set HTMLdoc = IE.Document
    Set link = Nothing
    i = 0
    While i < HTMLdoc.links And link Is Nothing
        If HTMLdoc.links(i).innerText = "S-C-BSS-1" Then Set link = HTMLdoc.links(i)
        i = i + 1
    Wend
    
    If Not link Is Nothing Then
        link.click
        While IE.Busy Or IE.ReadyState <> 4: DoEvents: Wend
    Else
        MsgBox "Link not found"
    End If
 
Upvote 0
Try adding this to your code. It uses HTML library data types, so you must set a reference to MS HTML Object Library, via Tools -> References in the VBA editor.
Code:
    Dim HTMLdoc As HTMLDocument
    Dim link As HTMLAnchorElement
    Dim i As Long
    
    'After IE navigation and page has loaded
    
    Set HTMLdoc = IE.Document
    Set link = Nothing
    i = 0
[B]    While i < HTMLdoc.links And link Is Nothing
[/B]        If HTMLdoc.links(i).innerText = "S-C-BSS-1" Then Set link = HTMLdoc.links(i)
        i = i + 1
    Wend
    
    If Not link Is Nothing Then
        link.click
        While IE.Busy Or IE.ReadyState <> 4: DoEvents: Wend
    Else
        MsgBox "Link not found"
    End If


Hi John,

Thanks for the speedy response. I've tried adding this in...but :

1. I get a Type mismatch and the above line is highlighted yellow. I'm guessing that it is because of the 's' at the end of 'links'. I removed the 's' (from both cases of 'links') and the Type mismatch goes, but...
2. I now get a Run-time error '438': Object doesn't support this property.

Any suggestions as to how to resolve this?

Thanks Bobby
 
Upvote 0
A minor amendment to John's code...

Code:
While i < HTMLdoc.Links[COLOR=#ff0000].Length[/COLOR] And link Is Nothing

However, your second error after removing the 's' from links suggests that you have not understood John's instructions. He stated that the code applies after using Internet Explorer to navigated to your URL and the page has loaded. If you have not done so and need help with it, post back.
 
Last edited:
Upvote 0
A minor amendment to John's code...

Code:
While i < HTMLdoc.Links[COLOR=#ff0000].Length[/COLOR] And link Is Nothing

However, your second error after removing the 's' from links suggests that you have not understood John's instructions. He stated that the code applies after using Internet Explorer to navigated to your URL and the page has loaded. If you have not done so and need help with it, post back.


Hi Domenic,

First error...no longer get the error message.

Second error...two things:

1. I have added the following before the script you guys have provided...Application.Wait Now + TimeValue("00:00:06"). No longer get the error message and all works good.
2. I tried adding in the While objIE.Busy Or objIE.ReadyState <> 4: DoEvents: Wend script instead of the above, but it errors.

Personally I prefer John's solution as I have to move across a number of webpages and so my script delays the next piece of code for a set number of seconds rather than wait for the page to load. Any solution for the original script from John?

Note, I have also found the following, but that doesn't work either!

While objIE.ReadyState <> READYSTATE_COMPLETE And objIE.ReadyState <> READYSTATE_LOADED
DoEvents
Wend

Thanks, Bobby
 
Upvote 0
2. I tried adding in the While objIE.Busy Or objIE.ReadyState <> 4: DoEvents: Wend script instead of the above, but it errors.

In John's code, Internet Explorer has been assigned to the object variable IE. However, you refer to objIE. Could this be the issue? If not, can you post the complete code for us to look at?
 
Upvote 0
Hey Domenic,

I don't think the objIE is causing the issue as I have that as my variable name in the script. Here's my script (and please don't beat me up if it seems rudamentary...I am only a beginner!)

Code:
Sub IE_WebPageLogOn()
 
Dim AC_Code As String
Dim AC_Name As String
Dim i As Long
Dim LogOn_ID As String
Dim LogOn_URL As String
Dim objIE As Object
Dim ObjCollection As Object
Dim Passcode As String
Dim RegNo As String
Dim WshShell
 
‘Closes all IE sessions.
Call IE_Sledgehammer
 
Set objIE = CreateObject("InternetExplorer.Application")
 
AC_Code = Range("LogOn_SheetName").Value
LogOn_ID = Range("LogOn_LogOnID").Value
LogOn_URL = Range("LogOn_URL").Value
'LogOn_URL = "https://retail.santander.co.uk/LOGSUK_NS_ENS/BtoChannelDriver.ssobto?dse_operationName=LOGON&dse_processorState=initial&redirect=S"
 
'Load Santander.com
With objIE
.Visible = True
.Navigate LogOn_URL
.Visible = True
End With
 
'>>>Inputs the User ID.
 
'WAIT TO LOAD – The un-commented script is what I am using in this section
'Application.Wait Now + TimeValue("00:00:03")
'XXX
Do Until Not objIE.Busy And objIE.ReadyState = 4
DoEvents
Loop
'XXX
'While objIE.Busy Or objIE.ReadyState <> 4: DoEvents: Wend
'XXX
'While objIE.ReadyState <> READYSTATE_COMPLETE And objIE.ReadyState <> READYSTATE_LOADED
'DoEvents
'Wend
 
objIE.Document.getElementsByName("infoLDAP_E.customerID")(0).Value = LogOn_ID
SendKeys ("{TAB}")
SendKeys ("{ENTER}")
 
'>>>Turns 'Number Lock' back On.
Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys "{NUMLOCK}"
 
'>>>Inputs the Passcode.
 
'WAIT TO LOAD – The un-commented script is what I am using in this section
Application.Wait Now + TimeValue("00:00:06")
'XXX
'Do Until Not objIE.Busy And objIE.ReadyState = 4
'DoEvents
'Loop
'XXX
'While objIE.Busy Or objIE.ReadyState <> 4: DoEvents: Wend
'XXX
'While objIE.ReadyState <> READYSTATE_COMPLETE And objIE.ReadyState <> READYSTATE_LOADED
'DoEvents
'Wend
 
objIE.Document.getElementsByName("authentication.PassCode")(0).Value = Passcode
 
'>>>Inputs the Registration Number and Enters through to main page.
objIE.Document.getElementsByName("authentication.ERN")(0).Value = RegNo
objIE.Document.getElementsByName("buttons.1")(0).Click
 
'>>>Selects the account to review.
 
'WAIT TO LOAD – The un-commented script is what I am using in this section
Application.Wait Now + TimeValue("00:00:04")
'XXX
'Do Until Not objIE.Busy And objIE.ReadyState = 4
'DoEvents
'Loop
'XXX
'While objIE.Busy Or objIE.ReadyState <> 4: DoEvents: Wend
'XXX
'While objIE.ReadyState <> READYSTATE_COMPLETE And objIE.ReadyState <> READYSTATE_LOADED
'DoEvents
'Wend
 
'>>>From Mr Excel...John...
Dim HTMLdoc As HTMLDocument
Dim link As HTMLAnchorElement
'Dim i As Long
   
'>>>Accounts page has loaded, identify the Hyperlink for the appropriate account to be interrogated and Click.
 
Set HTMLdoc = objIE.Document
Set link = Nothing
i = 0
While i < HTMLdoc.Links.Length And link Is Nothing
    If HTMLdoc.Links(i).innerText = AC_Code Then Set link = HTMLdoc.Links(i)
    i = i + 1
Wend
 
If Not link Is Nothing Then
    link.Click
 
'WAIT TO LOAD – The un-commented script is what I am using in this section
Application.Wait Now + TimeValue("00:00:04")
'XXX
'Do Until Not objIE.Busy And objIE.ReadyState = 4
'DoEvents
'Loop
'XXX
'While objIE.Busy Or objIE.ReadyState <> 4: DoEvents: Wend
'XXX
'While objIE.ReadyState <> READYSTATE_COMPLETE And objIE.ReadyState <> READYSTATE_LOADED
'DoEvents
'Wend
 
Else
    MsgBox "Link not found"
End If
 
'>>>Transactions page loaded, then appropriate dates to be input.
 
'WAIT TO LOAD – The un-commented script is what I am using in this section
Application.Wait Now + TimeValue("00:00:06")
'XXX
'Do Until Not objIE.Busy And objIE.ReadyState = 4
'DoEvents
'Loop
'XXX
'While objIE.Busy Or objIE.ReadyState <> 4: DoEvents: Wend
'XXX
'While objIE.ReadyState <> READYSTATE_COMPLETE And objIE.ReadyState <> READYSTATE_LOADED
'DoEvents
'Wend
 
'>>>On same page, input the From and To dates and then Click the 'Search' button (last bit isn't working yet)..
'objIE.Document.getElementsByName("searchForm.fromDate.day")(0).Value = 1
'objIE.Document.getElementsByName("searchForm.fromDate.month")(0).Value = 2
'objIE.Document.getElementsByName("searchForm.fromDate.year")(0).Value = 2019
'
'objIE.Document.getElementsByName("searchForm.toDate.day")(0).Value = "1"
'objIE.Document.getElementsByName("searchForm.toDate.month")(0).Value = "3"
'objIE.Document.getElementsByName("searchForm.toDate.year")(0).Value = "2019"
'
'objIE.Document.getElementsByClassName("button")(0).Click
 
'>>>On same page, identify the Hyperlink for 'Download transactions' and Click.
Set HTMLdoc = objIE.Document
Set link = Nothing
i = 0
While i < HTMLdoc.Links.Length And link Is Nothing
    If HTMLdoc.Links(i).innerText = "Download transactions" Then Set link = HTMLdoc.Links(i)
    i = i + 1
Wend
 
If Not link Is Nothing Then
    link.Click
'    While objIE.Busy Or objIE.ReadyState <> 4: DoEvents: Wend
Else
    MsgBox "Link not found"
End If
 
'WAIT TO LOAD – The un-commented script is what I am using in this section
Application.Wait Now + TimeValue("00:00:06")
'XXX
'Do Until Not objIE.Busy And objIE.ReadyState = 4
'DoEvents
'Loop
'XXX
'While objIE.Busy Or objIE.ReadyState <> 4: DoEvents: Wend
'XXX
'While objIE.ReadyState <> READYSTATE_COMPLETE And objIE.ReadyState <> READYSTATE_LOADED
'DoEvents
'Wend
 
'>>>Select Download To option as 'Microsoft Excel (XLS)'.
'objIE.Document.getElementsByName("downloadStatementsForm.typeFile")(0).selectedIndex = 1
 
With objIE.Document.getElementsByName("downloadStatementsForm.typeFile")(0)
.selectedIndex = 1
.FireEvent "onchange"
End With
 
'objIE.Document.getElementsByName("downloadStatementsForm.typeFile")(0).Value = "Microsoft Excel (XLS)"
'SendKeys ("{TAB}")
'SendKeys ("{ENTER}")
 
'WAIT TO LOAD – The un-commented script is what I am using in this section
'Application.Wait Now + TimeValue("00:00:04")
Do Until Not objIE.Busy And objIE.ReadyState = 4
DoEvents
Loop
'XXX
'While objIE.Busy Or objIE.ReadyState <> 4: DoEvents: Wend
'XXX
'While objIE.ReadyState <> READYSTATE_COMPLETE And objIE.ReadyState <> READYSTATE_LOADED
'DoEvents
'Wend
 
'>>>On same page, input the From and To dates to be downloaded.
objIE.Document.getElementsByName("downloadStatementsForm.fromDate.day")(0).Value = "1"
objIE.Document.getElementsByName("downloadStatementsForm.fromDate.month")(0).Value = "2"
objIE.Document.getElementsByName("downloadStatementsForm.fromDate.year")(0).Value = "2019"
 
objIE.Document.getElementsByName("downloadStatementsForm.fromDate.day")(0).Value = "1"
objIE.Document.getElementsByName("downloadStatementsForm.fromDate.month")(0).Value = "3"
objIE.Document.getElementsByName("downloadStatementsForm.fromDate.year")(0).Value = "2019"
 
'>>>On same page, Click the 'Download' button.
objIE.Document.getElementsByName("downloadStatementsForm.events.0")(0).Click
 
 
 
End Sub
 
Last edited by a moderator:
Upvote 0
Can you specify which line gives you an error, and which error it gives you?
 
Upvote 0
Basically...my preference would be to use code along the lines of what John initially provided, ie: While objIE.Busy Or objIE.ReadyState <> 4: DoEvents: Wend

At the moment I have tried various scripts that try to do the same thing as John's script, but none are working as expected, ie: wait until the page is loaded before doing the next step in the script. Hence I have had to use the more manual mthod of applying Application.Wait Now + TimeValue("00:00:06"). The problem with this is that sometimes the page takes longer to load than the number of seconds I have input...and at other times I'm having too wait for longer than necessary...all dependent (I guess) on the internet speeds I am getting at that time.

Hope that makes sense. At the moment, it works (most of the time), but isn't the most efficient script. So, please don't spend too long on this as I wouldn't want to waste your time.

Thanks for all your help.

Bobby
 
Upvote 0
Sorry, I don't see off hand why you're getting an error. Maybe John (or someone else) might be able to pinpoint the problem.
 
Upvote 0

Forum statistics

Threads
1,223,954
Messages
6,175,603
Members
452,658
Latest member
GStorm

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