One project I'm currently working on involves using a VBA macro in Excel to automate the process of navigating to specific web pages. Through this automation, I can select elements based on their ID and manipulate them accordingly on the site.
However, I've encountered a roadblock when attempting to 'click' a link or button that does not have an assigned ID. This particular link/button in question is a JavaScript element embedded within the HTML code:
<div>
<table class="table" id="ctl00_cphMaster_Results" style="width: 100%; border-collapse: collapse;" cellspacing="0">
<tbody><tr>
<th style="width: 70px; white-space: nowrap;" scope="col"><a href="javascript:__doPostBack('ctl00$cphMaster$Results','Sort$Indicator')">True?</a></th><th class="hidden" style="width: 100px;" scope="col"><a href="javascript:__doPostBack('ctl00$cphMaster$Results','Sort$Reference')">Reference</a></th><th scope="col"><a href="javascript:__doPostBack('ctl00$cphMaster$Results','Sort$Results')">Result Number</a></th><th class="hidden" scope="col"> </th><th scope="col"><a href="javascript:__doPostBack('ctl00$cphMaster$Results','Sort$Type')">Type</a></th><th class="hidden" scope="col"></th><th class="hidden" scope="col"> </th><th class="hidden" scope="col"> </th>
&...
I've made several attempts to interact with the unassigned link/button labeled as 'ResultNumber$0', but all my efforts so far have been in vain. Is there a way for me to automate Internet Explorer to click this JavaScript link/button successfully?
Sub ClickJavaLink()
Const URL = "https://myurl"
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.navigate URL
ieBusy ie
.Visible = True
For Each Item In ie.document.all
If Item.ID = "QuickSearchField" Then
Item.Value = 1
End If
If Item.ID = "QuickSearch" Then
Item.Value = "MyValue"
End If
If Item.ID = "btnSearch" Then
Item.Click
Exit For
End If
Next
ieBusy ie
For Each Item In ie.document.all
If Item.ID = "ResultNumber$0" Then
Item.Click
Exit For
End If
Next
ieBusy ie
End With
End Sub
Sub ieBusy(ie As Object) Do While ie.Busy Or ie.ReadyState < 4 DoEvents Loop End Sub