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
 
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.
Ok so it outputs a frame document. How would I look at this with VBA?

1662755213653.png
 
Upvote 0

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Ok so it outputs a frame document. How would I look at this with VBA?

1662755213653.png
Assuming the document you need is in the "main" frame, use this to get that document and then substitute IE.Document with HTMLdocin all the previous code I've posted.
VBA Code:
    Dim HTMLdoc As HTMLDocument
    Dim frame As HTMLWindow2
    Set frame = IE.document.frames("main")
    Set HTMLdoc = frame.document
Again, use Debug.Print to verify that you've got the correct document.
 
Last edited:
Upvote 0
Assuming the document you need is in the "main" frame, use this to get that document and then substitute IE.Document with HTMLdocin all the previous code I've posted.
VBA Code:
    Dim HTMLdoc As HTMLDocument
    Dim frame As HTMLWindow2
    Set frame = IE.document.frames("main")
    Set HTMLdoc = frame.document
Again, use Debug.Print to verify that you've got the correct document.
So I tried HTMLdoc.getElementById("cursorhand").FireEvent ("onclick") no luck. The debug just shows the same info I sent earlier. No difference.
 
Upvote 0
Not clear which Debug you're referring to. Did you try Debug.Print HTMLdoc.body.outerHTML? What does it display?

The full code should be:
VBA Code:
    #If VBA7 Then
        Dim clickEvent As DOMMouseEvent
    #Else
        Dim clickEvent As Object
    #End If
    Dim frame As HTMLWindow2
    Dim HTMLdoc As HTMLDocument
    Dim cursorHand As HTMLAnchorElement

    '.... after the web page has completely loaded in the IE object
    
    Set frame = IE.document.frames("main")
    Set HTMLdoc = frame.document
    Debug.Print HTMLdoc.body.outerHTML
        
    With HTMLdoc
        Set clickEvent = .createEvent("MouseEvent")
        clickEvent.initEvent "click", True, False
        'Either:
        Set cursorHand = .getElementById("cursorhand")
        'Or:
        'Set cursorHand = .querySelector("a.cursor")
    End With
    
    cursorHand.dispatchEvent clickEvent
    Debug.Print cursorHand.outerHTML
 
Upvote 0
Not clear which Debug you're referring to. Did you try Debug.Print HTMLdoc.body.outerHTML? What does it display?

The full code should be:
VBA Code:
    #If VBA7 Then
        Dim clickEvent As DOMMouseEvent
    #Else
        Dim clickEvent As Object
    #End If
    Dim frame As HTMLWindow2
    Dim HTMLdoc As HTMLDocument
    Dim cursorHand As HTMLAnchorElement

    '.... after the web page has completely loaded in the IE object
   
    Set frame = IE.document.frames("main")
    Set HTMLdoc = frame.document
    Debug.Print HTMLdoc.body.outerHTML
       
    With HTMLdoc
        Set clickEvent = .createEvent("MouseEvent")
        clickEvent.initEvent "click", True, False
        'Either:
        Set cursorHand = .getElementById("cursorhand")
        'Or:
        'Set cursorHand = .querySelector("a.cursor")
    End With
   
    cursorHand.dispatchEvent clickEvent
    Debug.Print cursorHand.outerHTML
No luck. I tried changing "main" frame to "contents" and both set cursorhand lines.

I also tried these as well which didnt work:
IE.document.frames("contents").getElementById("a.cursor").FireEvent ("onclick")
IE.document.frames("main").getElementById("a.cursor").FireEvent ("onclick")
IE.document.frames("contents").getElementById("cursor").FireEvent ("onclick")
IE.document.frames("main").getElementById("cursor").FireEvent ("onclick")
IE.document.frames("main").getElementById("cursor").Click
IE.document.frames("contents").getElementById("cursor").Click
IE.document.frames("main").getElementById("a.cursor").Click
IE.document.frames("contents").getElementById("a.cursor").Click

Debug output is the same. I'm using Debug.Print IE.document.body.outerHTML
 
Upvote 0
Debug output is the same. I'm using Debug.Print IE.document.body.outerHTML
No, you should be using Debug.Print HTMLdoc.body.outerHTML. The former outputs the frameset HTML that you posted. The latter should output the frame's HTML.
 
Upvote 0
No, you should be using Debug.Print HTMLdoc.body.outerHTML. The former outputs the frameset HTML that you posted. The latter should output the frame's HTML.
It doesnt output anything. main or contents
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

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