Reputation: 81
I'm having problems trying to click on a href into a webpage. The href is included in "a" tag and it begins like this "http://trial.xxxyyy". How to set a string that allows me to click on a href that begins with the before mentioned string? I think queryselector is the key but i'm not familiar with it.
P.s the page is quite empty so there's not too much html.
Upvotes: 0
Views: 423
Reputation: 21656
Try to use the [attribute^=value]
CSS selector, it will selects every element whose href attribute value begins with special value.
Sample code as below:
Public Sub ClickTest()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.Navigate2 "<the website url>"
While .Busy Or .readyState <> 4: DoEvents: Wend
ie.Document.querySelector("a[href^='https://www.bing']").Click
End With
End Sub
the website resource like this:
<a class="link" href="https://www.google.com/"> Google</a><br />
<a class="link" href="https://www.microsoft.com/en-us"> Microsoft</a><br />
<a class="link" href="https://www.bing.com/"> Bing</a><br />
<a class="link" href="https://www.bing.com/search?q=vba&qs=n&form=QBLH&sp=-1&pq=vba&sc=6-3&sk=&cvid=400A0A54265148289028B12BE5D9E3A4">Bing search</a>
[Note] If there have multiple elements match the filter, by using the above code, it will get the first item.
Upvotes: 2