首页 > 解决方案 > VBA 自动化 - Div 对象不可点击

问题描述

我目前正在 VBA 中创建网络自动化。我在单击“div”组件时遇到困难-> 它没有响应任何单击、onclick、mousedown 等。事件。我能够将 div 组件作为 VBA 中的对象“捕获”,但是当我尝试这些操作时,它没有响应任何操作。还有它的内部组件。

div 对象:

<div tabindex="0" class="SubForm SubForm-up" role="button" aria-pressed="false" style="width: 140px; height: 26px; float: right;">
<input tabindex="-1" role="presentation" style="width: 1px; height: 1px; overflow: hidden; position: absolute; z-index: -1; opacity: 0;" type="text">
<div class="html-face">הגדרות לחשבון מעבר</div>
</div>

我的代码:

Set frmPane = doc.getElementsByClassName("SubForm SubForm-up")(3)
       frmPane.onclick = frmPane.onmousedown
       frmPane.onclick = frmPane.onmouseup
       
       With frmPane
            .Focus
            '.setAttribute "display", "block"
            '.setAttribute "aria-expanded", "true"
            .Click
            .onclick = frmPane.onmouseup
            .FireEvent "onclick"
            .FireEvent "onmouseover"
            .FireEvent "onmousedown"
            .FireEvent "onmouseup"
            .FireEvent "onkeydown"
            .FireEvent "onkeypress"
            .FireEvent "onkeyup"
           ' .Blur
        End With
        
       
       Set heshbonMaavarObj = frmPane.getElementsByTagName("input")(0)
       With heshbonMaavarObj
            .Focus
            .Click
            .FireEvent "onclick"
            .FireEvent "onmouseover"
            .FireEvent "onmousedown"
            .FireEvent "onmouseup"
            .FireEvent "onkeydown"
            .FireEvent "onkeypress"
            .FireEvent "onkeyup"
            '.Blur
        End With
       
       Set heshbonMaavarObj = frmPane.getElementsByTagName("div")(0)
       With heshbonMaavarObj
            .Focus
            .Click
            .FireEvent "onclick"
            .FireEvent "onmouseover"
            .FireEvent "onmousedown"
            .FireEvent "onmouseup"
            .FireEvent "onkeydown"
            .FireEvent "onkeypress"
            .FireEvent "onkeyup"
            '.Blur
        End With

标签: htmlvbainternet-explorerautomationclick

解决方案


根据您的描述,我创建了一个简单的示例进行测试,我发现内联脚本可以很好地工作。不回复是什么意思?你能提供更多细节吗?

这是我的测试(使用 window.alert() 函数):

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

    url = "https://localhost:44315/Index.html"
    Set ie = CreateObject("InternetExplorer.Application")

    ie.Visible = True
    ie.navigate url

    While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE
        DoEvents
    Wend

    Set doc = ie.document
    Set div = ie.document.getElementsByClassName("SubForm SubForm-up")(0)
    div.FireEvent "onclick"
    ie.Quit

    Set ie = Nothing
End Sub

<div tabindex="0" class="SubForm SubForm-up" role="button" aria-pressed="false" style="width: 140px; height: 26px; float: right;" onclick="window.alert('Div element clicked...')">
        <input tabindex="-1" role="presentation" style="width: 1px; height: 1px; overflow: hidden; position: absolute; z-index: -1; opacity: 0;" type="text">
        <div class="html-face">הגדרות לחשבון מעבר</div>
    </div>

结果:

在此处输入图像描述


推荐阅读