Excel VBA – 当button具有相同的名称时,单击一个button

我有一个网站,其中所有的button都有相同的类名,button的唯一区分部分是“data-dojo-attach-point”。

我正在尝试点击该button或select该字段并input值。

<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正确的button来点击?

提前致谢!

这是一个例子。 我保存了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> 

并推出了以下代码:

 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 

正如您在即时窗口中所看到的, objNode.innerText是“Build to Address”,与具有data-dojo-attach-point="buildToAddress"目标节点相对应。

    Interesting Posts