Click <div> button that has no "onclick" method

Twirlzy_

New Member
Joined
Mar 19, 2021
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
I'm writing a macro to autograb data from a list of websites and one of my websites makes use of a toolbar. To pull up the download button dialog, I need to click a button on the toolbar labeled "Download." The "Download" button I am supposed to click however is not an actual button and has no effect if not errors when I attempt to click it from VBA. I have also attempted to click each of the <spans> under the anchor class but nothing worked their either.

Below is the HTML for the "button" if you can call it that. I also have my code below which grabs an element by classname but clicking it accomplishes nothing.

HTML:

<div class-"tabToolbarButton tab-widget download" role="button"
data-tb-test-id="download-ToolbarButton" id="download-ToolbarButton" tabindex="-1"
style="user-select: none; -webkit-tap-highlight-color: transparent;" title="Download">
<span class="tabToolbarButtonImg tab-icon-download"></span>
<span class="tabToolbarButtonText">Downloads</span>

VBA:

'Variables
Dim IE As InternetExplorer
Dim HTMLdoc As HTMLDocument
Dim DownloadButton As HTMLInputButtonElement
Set IE = New InternetExplorerMedium
With IE

.Visible = True
.Left = 25
.Top = 25
.Height = 700
.Width = 1300
AppActivate ("Internet Explorer")
.Navigate Site
IE_Wait IE
Set HTMLdoc = .Document
Application.Wait (Now + TimeValue("00:00:10"))
IE_Wait IE

Set DownloadButton = HTMLdoc.getElementsByClassName("tabToolbarButton tab-widget download")(0)
DownloadButton.Click '<-- ERRORS here as DownloadButton is not set to a real <button>

'CODE BELOW FINDS THE CORRECT BUTTON TO DOWNLOAD DATA
'NEED TO FIND DOWNLOAD BUTTON TO ENABLE VISIBLITY OF DIALOGBOX
Debug.Print HTMLdoc.getElementsByClassName("fppw03o low-density").Length

Site link has been omitted, and the IE_Wait function merely waits until the IE is done loading. IE_Wait absolutely works given I have used it multiple times for different pages. Application.Wait is merely used to ensure everything has loaded properly before running code.

Any help would be appreciated! Thank you

---

When hovering over the button, the top class changes to

<div class="tabToolbarButton tab-widget download hover"...<div>

Same with when it is clicked, it changes to

<div class="tabToolbarButton tab-widget download hover focus"...<div>

momentarily.

Nothing happens with:

ie.document.querySelector(".tab-icon-download + .tabToolbarButtonText").click

I attempted to click each of those elements, the anchor, .tab-icon-download, and both spans, but nothing would actually get clicked.

---

[List of all <div> Event Listeners][1] for the parent <div>. No onclick event to be seen and I am unsure how the <div> is even watched. .Focus in VBA errors out and I am stumped.




[1]:
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Use getElementById to get the div element and dispatchEvent or fireEvent to trigger the click. Sometimes the element might have a click event associated with it and other event(s) e.g. mousedown. Sometimes the parent element has event(s) you must trigger - examine the parent hierarchy in your browser's dev tools. These experiments are shown in this code:
VBA Code:
    Dim downloadDiv As HTMLDivElement
    
    #If VBA7 Then
        Dim clickEvent As DOMMouseEvent
        Dim mouseDownEvent As DOMMouseEvent
    #Else
        Dim clickEvent As Object
        Dim mouseDownEvent As Object
    #End If
    
    'Ensure page is fully loaded before executing these lines
    
    Set clickEvent = HTMLdoc.createEvent("MouseEvent")
    clickEvent.initEvent "click", True, False
    Set mouseDownEvent = HTMLdoc.createEvent("MouseEvent")
    mouseDownEvent.initEvent "mousedown", True, False
    
    Set downloadDiv = HTMLdoc.getElementById("download-ToolbarButton")
    
    'Either:
    downloadDiv.dispatchEvent clickEvent
    downloadDiv.dispatchEvent mouseDownEvent
    
    'Or:
    downloadDiv.FireEvent "onclick"
    downloadDiv.parentElement.FireEvent "onclick"
 
Upvote 0
Thanks so much for your reply, I tried exactly that code but nothing appears to happen. If it helps, from what I can tell the element has a "mousedown" event but nothing happened when calling it.

1616156477870.png
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top