VBA Click Website Dropdown

excely121

New Member
Joined
Sep 9, 2022
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm trying to get my vba code to click this button on a website that will drop down with information once clicked. Can't seem to get the correct code for it.. ive tried multiple variations of the following changing the Id and getElementBy around with no luck:

IE.Document.getElementById("cursorhand").FireEvent ("onclick")
IE.Document.getElementById("cursorhand").Click

I can't provide the website as you have to login to it.. but here is the website inspect element of the button:


1662730413268.png



Thanks
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Welcome to MrExcel forums.

Assuming your IE.Document.getElementById("cursorhand") correctly refers to the <A> anchor element shown, try dispatchEvent, something like this after loadng the page in IE. Needs reference to MS HTML Object Library.

VBA Code:
    #If VBA7 Then
        Dim clickEvent As DOMMouseEvent
    #Else
        Dim clickEvent As Object
    #End If
    Dim cursorHand As HTMLAnchorElement
        
    With IE.Document
        Set clickEvent = .createEvent("MouseEvent")
        clickEvent.initEvent "click", True, False
        Set cursorHand = .getElementById("cursorhand")
    End With
    
    cursorHand.dispatchEvent clickEvent
 
Upvote 0
Welcome to MrExcel forums.

Assuming your IE.Document.getElementById("cursorhand") correctly refers to the <A> anchor element shown, try dispatchEvent, something like this after loadng the page in IE. Needs reference to MS HTML Object Library.

VBA Code:
    #If VBA7 Then
        Dim clickEvent As DOMMouseEvent
    #Else
        Dim clickEvent As Object
    #End If
    Dim cursorHand As HTMLAnchorElement
       
    With IE.Document
        Set clickEvent = .createEvent("MouseEvent")
        clickEvent.initEvent "click", True, False
        Set cursorHand = .getElementById("cursorhand")
    End With
   
    cursorHand.dispatchEvent clickEvent

I get a user defined compile error on this: Dim clickEvent As DOMMouseEvent

I think the issue is my IE.Document.getElementById("cursor").Click is not referencing the button Id properly. Any idea what I should change it to?
Its within a table so I'm thinking I need to do something like this: IE.Document.getElementById("frmDrill:row:cursor").FireEvent ("onclick")
 
Upvote 0
I get a user defined compile error on this: Dim clickEvent As DOMMouseEvent
Have you added the reference to MS HTML Object Library, in Tools -> References?

I think the issue is my IE.Document.getElementById("cursor").Click is not referencing the button Id properly.
As I said, you must ensure you're referencing the correct element. I can only go off what you've posted. Try this:

VBA Code:
Set cursorHand = .querySelector("a.cursor")
which should be the A element with the onclick event, although you've highlighted the IMG element which doesn't have an onclick event.
 
Upvote 0
Have you added the reference to MS HTML Object Library, in Tools -> References?


As I said, you must ensure you're referencing the correct element. I can only go off what you've posted. Try this:

VBA Code:
Set cursorHand = .querySelector("a.cursor")
which should be the A element with the onclick event, although you've highlighted the IMG element which doesn't have an onclick event.
I've added the HTML reference now..

The highlight wasn't intentional - So I tried IE.Document.querySelector("a.cursor").FireEvent ("onclick") and the below code with no luck.

Dim clickEvent As Object
Dim cursorHand As HTMLAnchorElement
With IE.Document
Set clickEvent = .createEvent("MouseEvent")
clickEvent.initEvent "click", True, False
Set cursorHand = .getElementById("a.cursor")
End With
cursorHand.dispatchEvent clickEvent
 
Upvote 0
Is cursorHand referencing the correct element? Add this to find out:
VBA Code:
Debug.Print cursorHand.outerHTML
 
Upvote 0
Is cursorHand referencing the correct element? Add this to find out:
VBA Code:
Debug.Print cursorHand.outerHTML
The inspect element of the button is above. Should be "cursor" I believe. I've tried both with no luck. I ran with Debug.Print but it does nothing.. not sure how to use it
 
Upvote 0
The Debug.Print outputs to the Immediate window. It should display the HTML you expect for the element you want.
 
Upvote 0
The Debug.Print outputs to the Immediate window. It should display the HTML you expect for the element you want.
It didn't show anything - was blank. I tried referencing the src image or onclick function but no luck.
 
Upvote 0
Does this output what you expect?
VBA Code:
Debug.Print IE.document.body.outerHTML
It should output the HTML for the whole page (document), without the headers, which should include the bit of HTML you posted. If not, it means you aren't looking at the correct document and your HTML is in a frame document.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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