首页 > 解决方案 > 从下拉菜单中单击按钮 VBA

问题描述

我在单击下拉菜单中的按钮时遇到问题。这是HTML:

<div class="dropdown text-center">
   <button class="btn btn-secondary dropdown-toggle" aria-expanded="false" aria-haspopup="true" type="button" data-toggle="dropdown">
      <!-- react-text: 400 -->Process Path Group: <!-- /react-text --><!-- react-text: 401 -->ALL<!-- /react-text -->
   </button>
   <div class="dropdown-menu dropdown-menu-right" aria-labelledby="statusProcessPathGroupDropdown" style="left: 0px; top: 0px; position: absolute; transform: translate3d(206px, 37px, 0px);" x-placement="bottom-end"><button class="dropdown-item active" type="button">ALL</button><button class="dropdown-item" type="button">SINGLE</button><button class="dropdown-item" type="button">MULTI</button></div>
</div>

我想单击“全部”按钮,但无法到达那里单击它。我什么都试过了。这是我最后一次尝试。

Sub Macro()
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = True
  Set IE = New InternetExplorerMedium
  url1 = "url1"

With IE
   .Visible = True
   .navigate url1


End With

Set AvailableLinks = IE.document.getElementByTagName("button")
        For Each cLink In AvailableLinks
            If cLink.innerHTML = "ALL" Then
            cLink.Click
            End If

谢谢你的帮助。

标签: htmlexcelvba

解决方案


没有这样的事情.getElementByTagName()

您正在寻找的方法是.getElementsByTagName(),它返回一个 html 元素的集合。

同样在您的情况下,按钮元素的内部 html 是:

<!-- react-text: 400 -->Process Path Group: <!-- /react-text --><!-- react-text: 401 -->ALL<!-- /react-text -->

而不仅仅是ALL

所以,你可以选择类似的东西:

Set AvailableLinks = IE.document.getElementsByTagName("button")

For Each clink In AvailableLinks
    If clink.innerHTML = "<!-- react-text: 400 -->Process Path Group: <!-- /react-text --><!-- react-text: 401 -->ALL<!-- /react-text -->" Then
    clink.Click
    End If
Next clink

或者更简单:

Set AvailableLinks = IE.document.getElementsByTagName("button")

For Each clink In AvailableLinks
    If clink.innerText = "Process Path Group: ALL" Then
    clink.Click
    End If
Next clink

推荐阅读