简体   繁体   中英

Excel VBA click button in Internet Explorer with getElementsByClassName

I need to scrape an internal company site to extract some buried data. In order to do this, I need to click through a number of buttons to access each record. I am having trouble getting VBA to click one of the buttons on the page in question.

This is the button I'm trying to click:

<button type="button" tabindex="0" class="StandardButton OptionsButton" value="Options ▼" id="DDMenuaB823456B43A2C4C1AABF2" onclick="lastMenuEntered='823456B43A2C4C1AABF2';enterMenu(this,'823456B43A2C4C1AABF2','','1110111');this.focus();" onblur="{setTimeout(function() {leaveMenu(this,'823456B43A2C4C1AABF2');},200)}"> <span style="color:#0069A5;text-justify:distribute-all-lines;" class="fa fa-lg fa-cog"></span><span class="OptionsText">Options ▼</span></button>

I am trying to access it via class="StandardButton OptionsButton" but without success.

I have been working on this for a couple of days (new to VBA). I tried following Clicking a button in Internet Explorer using VBA (getElementsByClassName) as well as various other solutions here but with no success.

Dim rowsonpage As Integer 'integer variable we'll use as a counter
Dim optbtn As HTMLInputButtonElement    
For rowsonpage = 1 To 20 'loop 20 rows per page
   'click options
    Set optbtn = objIE.document.getElementsByClassName("StandardButton OptionsButton")(1)
    optbtn.Click
        For Each btn In optbtn
            btn.Click
            Exit For
        Next
Next rowsonpage

I'm getting Run-time error 91 with this code.

I've also tried with this (and many other permutations):

For Each aInput In objIE.document.getElementsByClassName("StandardButton OptionsButton")
If aInput.getAttribute("value") = "Options" Then
    aInput.Click
    Exit For
End If
Next aInput

Which doesn't give any errors, but does not click the button.

Any help greatly appreciated.

Edit : I forgot to mention that I will need to loop through 20 of these elements per page, times around 700 pages - each with a different id attribute, but with the same class names - hence wanting to select by class, not id. Any other ideas? Thanks for the comments so far.

Try this

IE.document.getElementById("DDMenuaB823456B43A2C4C1AABF2").click()

Or

IE.document.getElementById("DDMenuaB823456B43A2C4C1AABF2").FireEvent("onclick")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM