跟踪 HTML 元素 Class 与 VBA
Tracking HTML Element Class With VBA
我一直在网上搜索答案,但到目前为止找不到任何有用的答案。问题实际上和标题一样简单。我在网页中有一些元素,我希望我的宏仅在它们 have/not 具有特定的 class.
时才执行操作
更准确地说,当您在页面内移动时,这些项目会被隐藏并添加一个隐藏的 class,因为它们仍然可以找到 getelementbyid 我有点无法验证这是否正确元素是否可见(隐藏 class 已移除)。
我认为不需要任何代码?这只是一些简单的元素切换隐藏 class 我很好奇我是否可以用 vba.
检查它
非常感谢!
我想我会在回答中为您充实它,因为我留下的评论并没有提供太多信息。
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. .
我一直在网上搜索答案,但到目前为止找不到任何有用的答案。问题实际上和标题一样简单。我在网页中有一些元素,我希望我的宏仅在它们 have/not 具有特定的 class.
时才执行操作更准确地说,当您在页面内移动时,这些项目会被隐藏并添加一个隐藏的 class,因为它们仍然可以找到 getelementbyid 我有点无法验证这是否正确元素是否可见(隐藏 class 已移除)。
我认为不需要任何代码?这只是一些简单的元素切换隐藏 class 我很好奇我是否可以用 vba.
检查它非常感谢!
我想我会在回答中为您充实它,因为我留下的评论并没有提供太多信息。
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. .