【问题标题】:Excel VBA Internet Explorer Onclick to click drop down menuExcel VBA Internet Explorer Onclick 以单击下拉菜单
【发布时间】:2019-11-02 16:57:19
【问题描述】:

我正在尝试使用 Excel VBA 代码单击 Internet Explorer 中的下拉类型菜单。

这是 HTML 代码:

<div class="treeNodeStyle" id="trMenu_14" nowrap="" style="visibility: visible;">
<div class="treeNodeWrapperStyle" nowrap="">
<a class="treeInlineStyle" onclick="FolderExpand('trMenu','14');return false;" href="">
<img align="top" class="treeInlineStyle" src="/visimages/tree/plus.gif" border="0">
</a> 
<div class="treeSelectorStyle" onclick="selectNode('trMenu','14',false)" ondblclick="dblselectNode('trMenu','14')">
<img align="absmiddle" class="treeInlineStyleImg" src="/visimages/tree/folder.gif" border="0">
<div title="Finance" class="treeNodeTextStyle" nowrap="true"> Finance</div>

这是我尝试使用的 VBA 代码。试图了解我是否应该引用 ID 或类。

Dim btn as Object

For Each btn In IE.Document.getElementsByClassName("treeNodeStyle")
    If btn.innerText = "Finance" Then
        btn.Click
        Exit For
    End If
Next btn

我无法获取代码以在 HTML 中查找任何类型的元素。

【问题讨论】:

    标签: excel vba internet-explorer automation onclick


    【解决方案1】:

    我假设您想要展开文件夹,但这将有助于指出哪个元素需要接收点击。

    为此,我使用带有 * contains 运算符的 css 属性 = 值选择器通过其值的一部分来定位 onclick 属性

    ie.document.querySelector("[onclick*=FolderExpand]").click
    

    如果事件需要触发则

    ie.document.querySelector("[onclick*=FolderExpand]").FireEvent "onclick"
    

    可以应用类似的原则来定位具有onclickondblclick 属性的其他元素。

    金融元素可以通过其标题属性定位

    ie.document.querySelector("[title=Finance]")
    

    我不确定这是点击的目标。

    【讨论】:

      【解决方案2】:

      从您的 HTML 代码看来,Finance div 位于类名为 treeSelectorStyle 且具有 onclick 事件的父 div 内。

      因此您可以尝试使用其类名单击父 div 可能有助于解决问题。

      示例代码:

      Sub demo()
      
          Dim URL As String
          Dim IE As Object
          Set IE = CreateObject("InternetExplorer.Application")
          IE.Visible = True
          URL = "C:\Users\Administrator\Desktop\152.html"   'chnage the URL here...
          IE.Navigate URL
      
          Do While IE.ReadyState = 4: DoEvents: Loop
          Do Until IE.ReadyState = 4: DoEvents: Loop
      
          IE.document.getelementsbyclassname("treeSelectorStyle").Item(0).Click
          Set IE = Nothing
      End Sub
      

      此处基于示例代码,我单击 Item(0),但您需要更改具有 treeSelectorStyle 类的元素的索引号才能单击正确的元素。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-29
        • 2012-10-01
        • 1970-01-01
        • 2014-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多