Reputation: 13
I use VBA a lot, but I'm still a beginner when it comes to IE automation. I need to create a code to click a "button" on a specific website for my company. This button opens up a small menu where I need to select a few things (I'll deal with that later I guess). Thing is, the button doesn't show any ID or name, it's a span and I'm having trouble making it click it. Below is the HTML when I press F12 and inspect the element over the button:
<span class="search_for_watchers">
<a href="/watchers/new?project_id=pendencia" data-method="get" data-remote="true">Procurar por outros observadores para adiconar</a>
</span>
I've tried so many suggestions that I found over the internet.. I've tried getting the element by ID, name, class name, etc, but no success. Below is the code I'm using:
Sub Automate_IE_Load_Page()
Dim i As Long
Dim URL As String
Dim ie As Object
Dim objElement As Object
Dim objCollection As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
URL = "Examplesite.com"
ie.Navigate URL
Application.StatusBar = URL & " is loading. Please wait..."
Do While ie.READYSTATE = 4: DoEvents: Loop
Do Until ie.READYSTATE = 4: DoEvents: Loop
Application.StatusBar = URL & " Loaded"
botao = ie.Document.getElementsByClassName("search_for_watchers")
botao.Click
End Sub
It doesn't show any error.. it just doesn't open up the sub-menu I want to, therefore the button isn't clicked.
Thanks!
Upvotes: 1
Views: 336
Reputation: 7735
You could also loop through the tags and find the one you want to click, like so:
Set tags = ie.document.getElementsByTagName("a")
For Each tagx In tags
If tagx.innerText = "Procurar por outros observadores para adiconar" Then
tagx.Click
Exit For
End If
Next
Upvotes: 1
Reputation:
getElementsByClassName is plural; i.e. it represents a collection. The following represents the first object with this class then clicks the first anchor tag within.
ie.Document.getElementsByClassName("search_for_watchers")(0).getElementsByTagName("a")(0).click
If you want to assign an object var to the element and then use the var to click, you need to Set the var.
Upvotes: 1
Reputation: 43575
You enter endless loop here twice:
Do While ie.READYSTATE = 4: DoEvents: Loop
Do Until ie.READYSTATE = 4: DoEvents: Loop
E.g., it looks like this:
Do While ie.READYSTATE = 4
DoEvents
Loop
but the :
are instead of a new line in VBA.
Make sure to change a bit the loop, thus something like this:
Do While NOT ie.READYSTATE = 4: DoEvents: Loop
Upvotes: 1