【问题标题】:VBA GetElementsByClassName gets error 5002VBA GetElementsByClassName 得到错误 5002
【发布时间】:2021-02-17 22:08:20
【问题描述】:

我第一次问问题,所以我们开始..

我在 VBA 方面有一些经验,但现在我被要求对我一直在阅读的内容进行一些网页抓取,并在 HTML 方面进行一些研究.. 问题是我有一个我无法解决的问题.

我需要做的事情真的没有任何困难.. 只需点击按钮或输入表单等元素自动完成,但这是我无法访问的:

<div class="dojoPopupMenu2" style="left: 31px; top: 293px; z-index: 1001;" dojoattachpoint="containerNode">
 <ul dojoattachpoint="containerNode">
  <li class="dojoMenuItem2 " dojoattachevent="onMouseOver: onHover; onMouseOut: onUnhover; onclick: _onclick; onKey:onKey;" dojoinsertionindex="0"><span tabindex="-1" class="dojoMenuItem2Label">1</span></li>
 <li class="dojoMenuItem2 " dojoattachevent="onMouseOver: onHover; onMouseOut: onUnhover; onclick: _onclick; onKey:onKey;" dojoinsertionindex="1"><span tabindex="-1" class="dojoMenuItem2Label">2</span></li>
 <li class="dojoMenuItem2 " dojoattachevent="onMouseOver: onHover; onMouseOut: onUnhover; onclick: _onclick; onKey:onKey;" dojoinsertionindex="2"><span tabindex="-1" class="dojoMenuItem2Label">3</span></li>
 <li class="dojoMenuItem2" dojoattachevent="onMouseOver: onHover; onMouseOut: onUnhover; onclick: _onclick; onKey:onKey;" dojoinsertionindex="3"><span tabindex="-1" class="dojoMenuItem2Label">4</span></li>
 <li class="dojoMenuItem2" dojoattachevent="onMouseOver: onHover; onMouseOut: onUnhover; onclick: _onclick; onKey:onKey;" dojoinsertionindex=`enter code here`"4"><span tabindex="-1" class="dojoMenuItem2Label">5</span>
 </li>
</ul>
</div>

有一个名为“添加项目”的前一个按钮,当点击它时,您会看到一个带有数字 1 到 5 的下拉列表(上面的代码),这就是我需要选择的内容。这些数字取决于一些简单的条件

我已经尝试了这些选项,但似乎都不起作用

Dim ieApp As Object
Dim ieDoc As Object
Dim ieEl As HTMLDivElement
Dim ieEls As HTMLObjectElement
Dim direccion As String
Set ieApp = CreateObject("InternetExplorer.Application")

With ieApp
For Each element In .document.getElementsByTagName("div") 'Tried with the tag "li" as well
    if element.classname = "dojoMenuItem2" Then
        'set a variable with the number 1 to 5 from the list (depending on other condition)
    end if
Next
End With


'also tried this
Dim additems as HTMLDivElement
Set additems = ieDoc.GetElementsByClassName("dojoMenuItem2")(0)
'this gets Automate error

'also tried this
Dim additems as HTMLDivElement
Set additems = ieDoc.GetElementsByClassName("dojoPopupMenu2")(0)
'this gets Automate error

似乎没有任何效果,所以我不知道我没有看到什么。

谢谢!

【问题讨论】:

  • (FYI) 通常,您应该避免使用 .getElementsByTagName().getElementsByClassName()getElementsByName(),因为它们在返回“实时”节点列表时都会对性能产生影响。相反,请使用更现代的 .querySelector().querySelectorAll(),它们返回静态节点列表。
  • 您是否在调试器中逐步检查代码以确定错误发生在哪一行?
  • 有网址可以分享吗?
  • @ScottMarcus 我将阅读有关这些方法的更多信息,看看是否能找到解决方法

标签: html excel vba getelementsbyclassname


【解决方案1】:

您是否尝试过应用 CSS 属性选择器?

ieApp.document.querySelectorAll("[dojoinsertionindex]")  

属性选择器[dojoinsertionindex]将返回所有具有[dojoinsertionindex]属性的元素。

这会返回一个您可以索引的nodeList

或者循环的长度:

Dim aNodeList As Object, i As Long
Set aNodeList = ie.document.querySelectorAll("[dojoinsertionindex]")  
For i = 0 To aNodeList.Length-1
    Debug.Print aNodeList.item(i).innerText '< === as check
Next i

单击一个部分索引(从 0 开始为 1)

aNodeList.item(0).Click

HTML 示例中的 CSS 选择器:

【讨论】:

  • 感谢您的回答!我已经尝试过了,现在没有错误,但我得到一个空值..所以aNodeList 长度为零。不幸的是,该 URL 需要登录才能访问,因此我无法共享它..
  • 顺便说一句,这是在任何形式的表单/框架/iframe标签中吗?
  • 嘿QHarr!我有几天无法访问我的电脑。我从我们离开它的地方拿走了它,并且可以找到解决方案!我得到了名为“添加项目”的元素,一旦点击并显示选项 1、2、3、4、5,我可以很容易地得到我需要的数字。
  • 非常感谢您的帮助!我需要更多地了解 HTML :)
猜你喜欢
  • 1970-01-01
  • 2018-10-23
  • 1970-01-01
  • 2019-07-19
  • 2019-11-29
  • 1970-01-01
  • 2016-04-21
  • 2013-10-21
  • 1970-01-01
相关资源
最近更新 更多