How do I click a link on a web page using Excel VBA?

前端 未结 2 996
北恋
北恋 2020-11-30 13:31

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 StackOv

相关标签:
2条回答
  • 2020-11-30 13:50

    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:


    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
    
    0 讨论(0)
  • 2020-11-30 14:05

    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")   
    
    0 讨论(0)
提交回复
热议问题