Excel VBA - 当按钮名称相同时单击按钮
Excel VBA - Click a button when the buttons have the same name
我有一个网站,其中所有按钮都具有相同的 class 名称,按钮的唯一区别部分是 "data-dojo-attach-point"。
我正在尝试单击该按钮或 select 该字段并输入值。
<div class="btn btn-default" type="button" data-dojo-attach-point="searchAddress">Search</div>
<div class="btn btn-default" type="button" data-dojo-attach-point="buildToAddress">Build to Address</div>
即像这样
Set element = .document.getElementsByClassName("btn btn-default")
element.Item(0).Click
有谁知道我如何select点击正确的按钮?
提前致谢!
您可以使用 CSS 选择器,如下例中的 .querySelector()
:
Sub Test()
Dim objNode As Object
With CreateObject("InternetExplorer.Application")
.Navigate "file://C:\tmp.htm"
.Visible = True
Do While .Busy Or Not .readyState = 4: DoEvents: Loop ' wait IE
Do Until .document.readyState = "complete": DoEvents: Loop ' wait document
Set objNode = .document.querySelector("div[data-dojo-attach-point='buildToAddress']")
Debug.Print objNode.innerText ' "Build to Address"
objNode.Click
.Quit
End With
End Sub
我保存了 C:\tmp.htm
用于测试,内容如下:
<html>
<title>test</title>
<body>
<div class="btn btn-default" type="button" data-dojo-attach-point="searchAddress">Search</div>
<div class="btn btn-default" type="button" data-dojo-attach-point="buildToAddress">Build to Address</div>
</body>
</html>
这里还有一个例子,使用.getElementsByClassName()
和.getAttribute()
:
Sub Test()
Dim colNodes As Object
Dim objNode As Object
Dim strTarget As String
strTarget = "buildToAddress"
With CreateObject("InternetExplorer.Application")
.Navigate "file://C:\tmp.htm"
.Visible = True
Do While .Busy Or Not .readyState = 4: DoEvents: Loop ' wait IE
Do Until .document.readyState = "complete": DoEvents: Loop ' wait document
Set colNodes = .document.getElementsByClassName("btn btn-default")
For Each objNode In colNodes
If objNode.getAttribute("data-dojo-attach-point") = strTarget Then Exit For
Next
If Not objNode Is Nothing Then
Debug.Print objNode.innerText ' "Build to Address"
objNode.Click
End If
.Quit
End With
End Sub
如您在立即 window 中看到的 objNode.innerText
是 "Build to Address" 对应于具有 data-dojo-attach-point="buildToAddress"
.
的目标节点
我有一个网站,其中所有按钮都具有相同的 class 名称,按钮的唯一区别部分是 "data-dojo-attach-point"。
我正在尝试单击该按钮或 select 该字段并输入值。
<div class="btn btn-default" type="button" data-dojo-attach-point="searchAddress">Search</div>
<div class="btn btn-default" type="button" data-dojo-attach-point="buildToAddress">Build to Address</div>
即像这样
Set element = .document.getElementsByClassName("btn btn-default")
element.Item(0).Click
有谁知道我如何select点击正确的按钮?
提前致谢!
您可以使用 CSS 选择器,如下例中的 .querySelector()
:
Sub Test()
Dim objNode As Object
With CreateObject("InternetExplorer.Application")
.Navigate "file://C:\tmp.htm"
.Visible = True
Do While .Busy Or Not .readyState = 4: DoEvents: Loop ' wait IE
Do Until .document.readyState = "complete": DoEvents: Loop ' wait document
Set objNode = .document.querySelector("div[data-dojo-attach-point='buildToAddress']")
Debug.Print objNode.innerText ' "Build to Address"
objNode.Click
.Quit
End With
End Sub
我保存了 C:\tmp.htm
用于测试,内容如下:
<html>
<title>test</title>
<body>
<div class="btn btn-default" type="button" data-dojo-attach-point="searchAddress">Search</div>
<div class="btn btn-default" type="button" data-dojo-attach-point="buildToAddress">Build to Address</div>
</body>
</html>
这里还有一个例子,使用.getElementsByClassName()
和.getAttribute()
:
Sub Test()
Dim colNodes As Object
Dim objNode As Object
Dim strTarget As String
strTarget = "buildToAddress"
With CreateObject("InternetExplorer.Application")
.Navigate "file://C:\tmp.htm"
.Visible = True
Do While .Busy Or Not .readyState = 4: DoEvents: Loop ' wait IE
Do Until .document.readyState = "complete": DoEvents: Loop ' wait document
Set colNodes = .document.getElementsByClassName("btn btn-default")
For Each objNode In colNodes
If objNode.getAttribute("data-dojo-attach-point") = strTarget Then Exit For
Next
If Not objNode Is Nothing Then
Debug.Print objNode.innerText ' "Build to Address"
objNode.Click
End If
.Quit
End With
End Sub
如您在立即 window 中看到的 objNode.innerText
是 "Build to Address" 对应于具有 data-dojo-attach-point="buildToAddress"
.