3

I'm writing VBA code to obtain a ticker symbol from a user, navigate to a website, input the ticker symbol and click the appropriate link.

I researched this StackOverflow question and response, however, I don't have an innertext value to utilize.

My VBA code:

Sub clicklick()
Dim ie As Object
Dim form As Variant, button As Variant
Set ie = CreateObject("InternetExplorer.Application")
ticker = InputBox("Enter Ticker Symbol: ")

With ie
.Visible = True
.Navigate ("http://www.SITE_URL.com")

While ie.ReadyState <> 4
DoEvents
Wend

ie.document.getElementsbyName("sSrchTerm").Item.innertext = ticker

End With
End Sub

The link appears as follows in the page source:

<a class="hqt_button" href="javascript:void(0): onclick=HeaderBox.trySubmit()"></a>

The element doesn't seem to have a name, innertext or id. How can I click the link?

EDIT:

    Set Link = ie.document.getElementsByTagName("a") 
    For Each l In Link 
        If Link.classname = "hqt_button" Then 
            Link.Click 
            Exit For 
        End If 
    Next l 
Community
  • 1
  • 1
Mutuelinvestor
  • 3,384
  • 10
  • 44
  • 75
  • `GetElementsByTagTame("a")`, then iterate that collection checking to ensure that the element's `.href` is consistent and/or the class is "hqt_button"? – David Zemens Jul 08 '14 at 17:57
  • class = "hqt_button", href= "javascript:void(0):..." – David Zemens Jul 08 '14 at 18:04
  • So something like the Edit above. – Mutuelinvestor Jul 08 '14 at 18:08
  • I think you're on the right track but try `If l.classname = "hqt_button" Then...` and then you need `Next l` or simply `Next` -- not `Next link`. – David Zemens Jul 08 '14 at 18:15
  • Made those changes, but got a Run-time 438: Object doesn't support this property or method. Doesn't seem to like this line of code:If link.classname = "hqt_button" Then – Mutuelinvestor Jul 08 '14 at 18:18
  • It works, had to change link to l for both l.classname and l.click. If you care to submit a formal answer, I be happy to give you the check mark. Really appreciate it. Thanks – Mutuelinvestor Jul 08 '14 at 18:21
  • ahh yes I forgot to mention `l.Click`. I will write an answer for you. Glad I could help! – David Zemens Jul 08 '14 at 18:29
  • I know this is an old thread but does anyone care to elaborate on what "l" or "Link" are defined or declared as? Its not too clear and I need to do something similar and am getting the same Run time error 438. Thanks – Dan May 02 '19 at 13:36

2 Answers2

10

Try getting the collection of anchor tags, with:

GetElementsByTagName("a")

Then, iterate that collection using as much logic as you can to ensure you're clicking the right button.

For each l in ie.document.getElementsByTagName("a") 
    If l.ClassName = "hqt_button" Then
        l.Click
        Exit For
    Next

If there are multiple anchors with the same classname, you could do:

    If l.ClassName = "hqt_button" AND l.Href = ""javascript:void(0): onclick=HeaderBox.trySubmit()" Then
        l.Click
        Exit For
    Next

Alternatively

If you are using IE9+ you could use the GetElementsByClassName method.

GetElementsByClassName("hqt_button")   
David Zemens
  • 53,033
  • 11
  • 81
  • 130
1

You could also use a CSS selector of

a.hqt_button[href='javascript:void(0): onclick=HeaderBox.trySubmit()']

The "." means className. [] encloses an attribute. The above says className hqt_button with attribute href with value 'javascript:void(0): onclick=HeaderBox.trySubmit()', inside of an a tag.


CSS query:

CSS query


Syntax in VBA:

CSS selectors are applied via the .querySelector method of the HTMLDocument.

ie.document.querySelector("a.hqt_button[href='javascript:void(0): onclick=HeaderBox.trySubmit()']").Click
QHarr
  • 83,427
  • 12
  • 54
  • 101