单击具有<span>值的按钮

时间:2015-08-09 18:58:12

标签: html vba button

我这几天一直在研究这个问题。我正在尝试浏览网站。我的下一步是单击一个按钮。不幸的是,URL取决于之前的选择,所以我不能只关注链接。

以下是我要点击的按钮的HTML:

<div class="buttons">
  <a class="greybutton" href="/pro/workouts/wizard?clientId=58806">
    <span>Create a New Workout</span>
  </a>
</div>

目前,我登录一个网站,在搜索框中输入一个值,弹出新信息,然后选中该按钮点击一个按钮。现在,在新页面上,我想点击另一个按钮,但无法弄明白。

这是我的VBA代码:

Sub TestWebsite()

Set ie = CreateObject("InternetExplorer.application")

ie.Visible = True
ie.Navigate ("https://functionalmovement.com/login?return=%2F" & ActiveCell)
Do
    If ie.readyState = 4 Then
        ie.Visible = True
        Exit Do
    Else
        DoEvents
    End If
Loop
    ie.Document.forms(0).all("Username").Value = Range("B3")
    ie.Document.forms(0).all("Password").Value = Range("B4")
Do While ie.Busy
Loop
    ie.Document.forms(0).submit
Do While ie.Busy
Loop
    Application.Wait (Now + TimeValue("0:00:01"))
    ie.Navigate ("http://functionalmovement.com/pro/clients" & ActiveCell)
Do
    If ie.readyState = 4 Then
        ie.Visible = True
        Exit Do
    Else
        DoEvents
    End If
Loop
ie.Document.forms(1).all("gvClients$DXFREditorcol1").Value = Range("B6")
ie.Document.forms(1).all("gvClients$DXFREditorcol1").Select

SendKeys String:="{enter}", Wait:=True

Application.Wait (Now + TimeValue("0:00:03"))

SendKeys String:="{tab}"
SendKeys String:="{tab}"
SendKeys String:="{tab}"
SendKeys String:="{tab}"
SendKeys String:="{tab}"
SendKeys String:="{enter}"

Application.Wait (Now + TimeValue("0:00:10"))

这是我希望代码点击按钮的位置。

1 个答案:

答案 0 :(得分:1)

由于您尝试点击的链接没有id,因此您需要按标记名称或类名称查找。

如果<span>元素始终包含该文本,您可以使用GetElementsByTagName()返回所有<span>元素,然后找到与显示的文本匹配的元素("Create a New Workout"

找到<span>后,您可以使用ParentElement属性访问其父级,然后调用Click()函数。

这应该可以解决问题:

Dim e
For Each e In ie.document.getElementsByTagName("span")
    If e.InnerText = "Create a New Workout" Then

        ' Found the <span>. Now click its parent (<a>)...           
        e.ParentElement.Click
        Exit For

    End If
Next
相关问题