【问题标题】:Tracking HTML Element Class With VBA使用 VBA 跟踪 HTML 元素类
【发布时间】:2020-05-26 23:48:31
【问题描述】:

我一直在网上搜索答案,但到目前为止找不到任何有用的东西。问题实际上就像标题一样简单。我在网页中有一些元素,我希望我的宏仅在它们具有/不具有特定类时执行操作。

更准确地说,每当您在页面内移动时,这些项目都会被隐藏并添加一个隐藏类,因为它们仍然可以找到 getelementbyid 我有点无法验证该元素是否可见(隐藏类被删除)或不。

我认为不需要任何代码?这只是切换隐藏类的一些简单元素,我很好奇是否可以使用 vba 进行检查。

提前非常感谢!

【问题讨论】:

  • 如果可以通过id获取元素,那么可以使用classname吗?
  • 也许,试试 Autoit 和 IE.au3
  • 你可以用VBA来做,在网页的不同点跟踪document.getElementsByClassName("your class name").length。我建议进入一个子过程,并将其设置为手表,然后通过网页触发元素类更改。也可以在监视窗口展开枚举,看到HTMLCollection中的元素,但是UI不厚道。
  • @Xenobiologist 我不能使用未经我公司批准的工具。
  • @Nathan_Sav 我可以通过 id 获取元素。那么,您是否建议我选择带有 id 的元素,然后检查该元素是否具有某个特定的类?所以我可以循环直到它得到这个类。听起来不错,但如何检查元素的类名?

标签: vba internet-explorer automation ie-automation


【解决方案1】:

我想我会在回答中为您充实它,因为我留下的评论并没有提供太多信息。

Set IE = CreateObject("InternetExplorer.Application")
Dim URL as String
URL = 'your URL here

Dim HTMLdoc As HTMLDocument

With IE
    .Silent = True
    .Visible = True
    .Navigate URL

    Do Until .readyState = 4
        DoEvents
    Loop

    Set HTMLdoc = .Document
End With

Dim hiddenElements As HTMLElementCollection
Set hiddenElements = HTMLdoc.getElementsByClassName("your class name") 'add this to a watch or inspect your locals
'Alternatively, you can print them all to the immediate window
Dim hiddenElement as Variant
For Each hiddenElement in hiddenElements
    Debug.Print hiddenElement.Value 'or .innerHTML or whatever you want here
Next hiddenElement

如果您正在单步执行代码,这很有效,但使用HTMLDocument 和readyState 可能会因为网页的实时更新而变得麻烦,所以MSXML.XMLHTTP60 is preferred.。

【讨论】:

  • 非常感谢您的回答和您的时间!据我了解,您建议获取隐藏元素列表并在其中循环查看哪些元素被值或其他识别特征隐藏。例如,此功能可以是 id 吗?有没有办法检查这个;
     Dim hiddenElements As HTMLElementCollection Set hiddenElements = HTMLdoc.getElementsByClassName("your class name") For Each hiddenElement in hiddenElements Debug.Print hiddenElement.id Can I see the id of an element like this? Next hiddenElement 
  • 我已经测试过了,是的,我可以做到。非常感谢你的想法!
猜你喜欢
  • 2019-10-09
  • 1970-01-01
  • 1970-01-01
  • 2014-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多