Reputation: 11
I'm new to VBA and have stumbled upon this one problem where I coultnt use vba code to automatically click on a "Submit" button from a website. I have tweaked my code many times but it always skipped the line "e.click". Below is my recent code and an image of the website's elements.
Hope someone can shed some lights here.
Set tags = objIE.Document.getElementById("alltab").getElementsByTagName("a")
For Each e In tags
If e.getAttribute("alt") = "Submit a Contract" Then
e.Click
End If
next
Upvotes: 1
Views: 98
Reputation: 21343
Please check your code, from your screenshot it seems that the hyperlink (html a tag) not inside the alltab table.
Please try to find the table by the class name and modify your code to add an ID property:
Find the table by ID property (add an ID property for the second table):
Set tags = doc.getElementById("table id").getElementsByTagName("a")
For Each e In tags
If e.getAttribute("alt") = "Submit a Contract" Then
e.Click
End If
Next e
or
Find the table by class name:
Set tags = doc.getElementsByClassName("belowDealButtonBox")(0).getElementsByTagName("a")
For Each e In tags
If e.getAttribute("alt") = "Submit a Contract" Then
e.Click
End If
Next e
Upvotes: 0
Reputation: 84455
You can simply use an attribute = value selector for the alt
attribute. Nice and fast. You don't want to loop an entire collection if you don't have to. Also, in any loop you would want to Exit For
after found I believe.
objIE.document.querySelector("[alt='Submit a Contract']").click
Upvotes: 1