如何使用VBA单击HTML内部文本

时间:2019-07-18 12:56:41

标签: html vba web-scraping automation

如何通过VBA单击“ li”的内部文本? rhef是动态的。 我有3页,在加载表单时,默认情况下页面为1,我必须单击没有2页。 我粘贴了以下代码,当我逐步查看时,行正在遍历循环,并传递“ difHTMLA.Click”行,但既不会引发错误,也不会重新加载页面。请帮助

Set difHTMLAs = doc.getElementsByTagName("li")
    For Each difHTMLA In difHTMLAs
        If difHTMLA.getAttribute("classname") = "swfPagingOtherPageArea swf-left" Then
            If CStr(Trim(difHTMLA.innerText)) = "2" Then
            difHTMLA.Click
            End If
        End If
      Next difHTMLA
    Exit For

<ul class="swfPaging">    
        <li> 
          &lt;
        </li>  
            <li class="swfPagingSelectedPageArea swf-left">
              1
            </li>
            <li class="swfPagingOtherPageArea swf-left">       
              <a href="/wps/myportal/dynamic URL/">
                2
              </a>
            </li>    
            <li class="swfPagingOtherPageArea swf-left">
              <a href="/wps/myportal/dynamic URL/">
                3
              </a>
            </li>

1 个答案:

答案 0 :(得分:0)

您可以使用css :nth-of-type伪选择器来定位父li的类。通过querySelector应用CSS选择器可返回第一个匹配项,或通过querySelectorAll应用可返回所有匹配项。然后,您希望li中的子类a标签为swfPagingOtherPageArea类。下方的.是一个CSS类选择器。它根据元素的类属性名称选择元素。这基于提供的html。

ie.document.querySelector(".swfPagingOtherPageArea:nth-of-type(3) a")

第2页

ie.document.querySelector(".swfPagingOtherPageArea:nth-of-type(4) a")

第3页

那是从起始页开始的。

在看不到页面的情况下,由于元素可能会更改,因此很难建议循环页面。例如,您可能会发现,在第二页上时,您不能使用.swfPagingOtherPageArea:nth-of-type(4) a来获取第三页,因为第三页现在是.swfPagingOtherPageArea:nth-of-type(3) a,即活动页的类名更改,因此使用的正确索引也更改


如果仍然没有触发,则可能需要在元素上触发FireEvent“ onclick”

ie.document.querySelector(".swfPagingOtherPageArea:nth-of-type(3) a").FireEvent "onclick"

如果仍然失败,您可以尝试将事件添加到节点,如here