【问题标题】:excel VBA how to click on onclick events of a div on IE11excel VBA如何在IE11上单击div的onclick事件
【发布时间】:2019-05-05 02:09:02
【问题描述】:

我已经搜索和搜索,但没有找到解决此问题的任何解决方案,

如何触发 div 中的 onclick 事件?通过使用它的标题和 getattribute 测试它,我得到了一切,但它就是不会点击。

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

网络代码

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

我还看到一些网站说 Fireevent 由于某种原因不能与 IE11 一起工作,还有其他替代方案吗?

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    div.FireEvent "onclick" 应该可以工作(下面是用 IE11 测试的示例)。 也许你的 div 是空的(不包含任何可以点击的地方?)。

    用于测试代码here的HTML。


    ' 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
            DoEvents
        Wend
    
        Set doc = ie.document
        Set div = ie.document.getElementById("btn_header")
    
        div.FireEvent "onclick"
        ie.Quit
    
        Set ie = Nothing
    End Sub
    

    【讨论】:

    • 太棒了,所以我必须调用 HTMLDivElement 来完成这项工作,谢谢!
    【解决方案2】:

    您也可以尝试直接调用脚本:

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

    【讨论】:

    • 是的!!!!我一直在寻找几个小时来弄清楚如何做到这一点。你一口气解决了我的很多问题。
    猜你喜欢
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    • 2019-11-02
    • 2014-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多