【问题标题】:Click a button with <span> value单击具有 <span> 值的按钮
【发布时间】:2015-08-09 18:58:11
【问题描述】:

我已经为此工作了几天。我正在尝试浏览一个网站。我的下一步是单击一个按钮。不幸的是,该 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"))

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

【问题讨论】:

    标签: html vba button


    【解决方案1】:

    由于您尝试点击的链接没有id,您需要通过标签名或类名找到它。

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

    找到&lt;span&gt; 后,您可以使用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
    

    【讨论】:

    • 太棒了,谢谢!这非常有效,并帮助我解决了其他一些问题。
    猜你喜欢
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多