Home » excel » excel VBA how to click on onclick events of a div on IE11

excel VBA how to click on onclick events of a div on IE11

Posted by: admin April 23, 2020 Leave a comment


i have searched and searched but have not find any solution to resolve this,

How do i fire an onclick event that is within a div? I am getting everything right by testing it with its title and getattribute but it just wouldn’t click.

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

The Web code

    <div id='btn_header' title='BTN' onclick="window.open('http://www.google.com','_blank');"></div>

I also saw some site saying Fireevent doesn’t work with IE11 for some reason, is there any other alternatives to it ?

How to&Answers:

div.FireEvent "onclick" should work (following example tested with IE11).
Maybe your div is empty (contains nothing where could be clicked on?).

HTML used to test the code here.

' Add reference to Microsoft Internet Controls (SHDocVw)
' Add reference to Microsoft HTML Object Library

Sub TriggerDivClick()
    Dim ie As SHDocVw.InternetExplorer
    Dim doc As MSHTML.HTMLDocument
    Dim div As HTMLDivElement
    Dim url As String

    url = "file:///c:/temp/divOnClick.html"
    Set ie = New SHDocVw.InternetExplorer

    ie.Visible = True
    ie.navigate url

    While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE

    Set doc = ie.document
    Set div = ie.document.getElementById("btn_header")

    div.FireEvent "onclick"

    Set ie = Nothing
End Sub


You might also try to call the script direct with:

IE.document.parentWindow.execScript "window.open('http://www.google.com','_blank');", "javascript"