Reputation: 45
I have a macro in VBA which uses Excel to automate navigating to a web-page.
I am able to select elements by their ID and click or insert values to automate the site.
I am stuck however when trying to 'click' a link/button which does not seem to have any ID. It is a JavaScript link:
<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>
</tr><tr>
<td align="center"><a href="Summary02.aspx?"><img title="Accepted" id="true" style="border: currentColor; border-image: none;" alt="Accepted" src="/Common/Graphics/tick_green.gif"></a><img id="Accepted" style="border: currentColor; border-image: none;" src="/Common/Graphics/tick_spacer.gif"><img id="Accepted" style="border: currentColor; border-image: none;" src="/Common/Graphics/tick_spacer.gif"></td><td class="hidden">MyValue</td><td><a href="javascript:__doPostBack('ctl00$cphMaster$Results','ResultNumber$0')">ResultNumber</a></td>
</tr>
</tbody></table>
I have attempted to click 'ResultNumber$0' or trigger it in some way but so far have been unsuccessful. How can I automate IE to click this javascript link/button? My code so far:
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
'code working up to this point - below code is attempt to click the javascript link but does not work
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
Upvotes: 2
Views: 2158
Reputation: 84465
There is a better, faster, way. Use an attribute = value css selector with contains operator
ie.document.querySelector("[href*='ResultNumber$0']").click
Single line; no looping; reduced code complexity.
Upvotes: 2
Reputation: 1697
You can reach the anchor elements by first accessing the table which conveniently enough has a unique ID
.
Here's how you can loop through all anchor elements inside that table:
Dim anc As HTMLAnchorElement
For Each anc In ie.Document.getElementById("ctl00_cphMaster_Results").getElementsByTagName("a")
Debug.Print anc.innerText
'anc.click 'uncomment this to click on all links
Next anc
For demonstration purposes the above code prints the text of these elements in the immediate window.
To access a specific element you can use the element's index. For example to access the first element in the table you can do it like so:
ie.Document.getElementById("ctl00_cphMaster_Results").getElementsByTagName("a")(0).click
To access the second one you just replace 0
with 1
and so on.
Another way to access a specific element would be to use its href
attribute:
Dim anc As HTMLAnchorElement
For Each anc In ie.document.getElementById("ctl00_cphMaster_Results").getElementsByTagName("a")
If anc.href = "javascript:__doPostBack('ctl00$cphMaster$Results','ResultNumber$0')" Then
Debug.Print anc.innerText
'anc.click
End If
Next anc
You will need to add a reference (VBE>Tools>References) to Microsoft HTML Object Library
.
Upvotes: 1
Reputation: 3845
You can try locating the href
element on the page like this :
For Each item In Ie.Document.getAttribute("href", 2)
If item.href ="javascript:__doPostBack('ctl00$cphMaster$Results','ResultNumber$0')" Then
item.Click
Exit For
End If
Next item
Let me know if that works.
Upvotes: 0