使用VBA单击按钮

时间:2017-09-21 06:50:42

标签: vba button

我试图从网站上提取信息,但不能为我的生活弄清楚如何点击按钮。我已经获得了输入信息的代码。这是网站的html。任何帮助将不胜感激。

<p role="button" tabindex="0" class="fmtbutton" onkeypress="finddistancebetweenaandb(document.forms['inp']['pointa'].value,document.forms['inp']['pointb'].value);" onclick="finddistancebetweenaandb(document.forms['inp']['pointa'].value,document.forms['inp']['pointb'].value);">&nbsp;Show&nbsp;</p>

这是我到目前为止的完整代码。

    Sub RoundedRectangle1_Click()
Dim eRow As Long
Dim ele As Object
Dim objIE As Object

Set objIE = CreateObject("InternetExplorer.Application")
FranchiseAddress = Range("B2").Value
Movefrom = Range("B3").Value
moveto = Range("B4").Value

With objIE
    .Visible = True
    .navigate "https://www.freemaptools.com/how-far-is-it-between.htm"
Do While .Busy Or _
.readyState <> 4
DoEvents
Loop
Set d = .Document.getElementsByName("onoffswitch")
d.Item(0).Click
Set f = .Document.getElementsByName("pointa")
f.Item(0).Value = FranchiseAddress
Set a = .Document.getElementsByName("pointb")
a.Item(0).Value = Movefrom

&#39;这就是我在Stuck&#39;

的地方
End With
End Sub

我已尝试在此网站上找到多个解决方案以解决许多问题,但这些问题似乎都无法解决。

谢谢。

2 个答案:

答案 0 :(得分:0)

将此添加到您的代码

Dim aaa As Object
Set aaa = .Document.getElementsByClassName("fmtbutton") ' this returns 4 objects, your button is the first one
aaa(0).Click

答案 1 :(得分:0)

您可以稍微优雅一点,并使用CSS选择器定位属性:

.document.querySelector("[role=button]").Click

attribute = value选择器[role=button]查找属性为role且元素值为button的元素。页面上只有一个,因此不需要循环。