【问题标题】:How to target CSS button elements properly with VBA Selenium when they don't have any IDs?当没有任何 ID 时,如何使用 VBA Selenium 正确定位 CSS 按钮元素?
【发布时间】:2019-02-13 22:24:11
【问题描述】:

我正在尝试定位并单击需要我打开下拉菜单的站点上的按钮,选择列表中的选项,然后单击应用。我已经得到了它的大部分方式,但我似乎无法让它点击按钮。这是我正在使用的相关 HTML 的链接https://pastebin.com/n5hJY3ua。而按钮的完整Xpath是:/html/body/div[5]/div[3]/div/button[1]

我尝试了多种方法来定位按钮,例如:

.FindElementByXPath("//div[@button='Apply']").Click

.FindElementByClass("applyBtn btn btn-small btn-success").Click

.FindElementByXPath(".//div[@button, 'Apply']").Click

Option Explicit

Public Sub ClickDate()
    Dim t As Date
    Dim ele As Object
    Dim driver As New ChromeDriver
    Dim post As WebElement
    Dim i As Integer
    Dim mysheet As Worksheet        

    Const MAX_WAIT_SEC As Long = 10
    Const INURL = "https://ss3.shipstation.com/#/dashboard"
    Const URL = "https://ss3.shipstation.com/"

    Set mysheet = Sheets("Sheet1")

    With driver '<==log into shipstation
        .Start "Chrome"
        .get URL
        t = Timer
        Do
            On Error Resume Next
            Set ele = .FindElementById("username")
            On Error GoTo 0
            If Timer - t > MAX_WAIT_SEC Then Exit Do
        Loop While ele Is Nothing

        If ele Is Nothing Then Exit Sub
        ele.SendKeys "Username"
        .FindElementById("password").SendKeys "Password"
        .FindElementById("btn-login").Click
    End With


    With driver '<==select todays date
        .get INURL  
        Dim drf As Object
        t = Timer

        Do
            Set drf = driver.FindElementsByCss(".col-sm-4 h2")
            If Timer - t > MAX_WAIT_SEC Then Exit Do
        Loop While drf.Count = 0

        If drf.Count > 0 Then
            .FindElementByClass("display-date").Click

            With .FindElementByXPath("//html/body/div/*/ul/li[1]")
                .Click
            End With

            .FindElementByXPath("//div[contains(@button, 'applyBtn btn btn-small btn-success')]").Click
        End If

        i = 2
        Dim item As Object, nodeList As Object, r As Long
        t = Timer

        Do
            Set nodeList = driver.FindElementsByCss(".col-sm-4 h2")
            If Timer - t > MAX_WAIT_SEC Then Exit Do
        Loop While nodeList.Count = 0

        If nodeList.Count > 0 Then
            For Each item In nodeList
                r = r + 1
                ActiveSheet.Cells(2, r) = item.Text
            Next
        End If
    End With
End Sub

我需要它来单击应用,从而使网站搜索今天的日期,然后获取数据并将其显示在 Excel 工作表上。除了能够单击按钮外,一切正常。它正在回到我身边

运行时错误“7”:NoSuchElementError。

【问题讨论】:

  • 听起来您可能需要使用Selenium.actions 将几个动作串在一起,这在涉及下拉菜单时经常需要。我对 VBA 不是很熟悉,但也许这个方向会有所帮助。
  • 我收回我之前的评论,我认为 Kajal 的选择器调整可能就是您所需要的。如果没有,请更新我们:)

标签: excel vba selenium selenium-webdriver


【解决方案1】:

请尝试这两种方法中的任何一种。它应该可以工作。

findElementByXPath("//button[contains(text(), 'Apply')]")

或者

findElementByXPath("//button[text()[contains(.,'Apply')]]")

或者

driver.findElementByCssSelector("button.applyBtn")

如果这行得通,请告诉我。

【讨论】:

  • 第一个导致运行时错误“32”:无效的选择器错误。运行时错误“438”的其他结果:对象不支持此属性或方法。
  • 我已经修改了答案,请立即尝试。
  • 非常感谢到目前为止的帮助。第一个似乎正在选择并单击按钮,但我的电子表格数据没有正确更新。例如,在将其更改为今天的日期之前,它默认显示过去 30 天,这是它当前正在提取的数据(带有您的答案),而不是在选择“今天”后应该自动填充的新更新信息并且单击“应用”按钮。在自动填写“今天”信息之前延迟获取数据的最佳方法是什么。有关更多详细信息,请参阅其他相关 HTML:pastebin.com/X1N2SYLz
  • 好的。让我检查一下。请您点击接受按钮接受我的回答。
  • 您需要等待元素出现在页面中,或者您可以设置一些隐式延迟。我不知道如何使它成为 selenium VBA。我擅长 selenium Java 和 Python。我必须检查语法然后才能说些什么。正如你所说,无法单击我试图帮助的按钮你。
【解决方案2】:

您可以进一步减少 css 选择器并使用单个类选择器。 css选择器方法会比xpath更快:

driver.findElementByCss(".applyBtn").click

对于 VBA selenium basic,正确的语法是 findElementByCss

【讨论】:

    猜你喜欢
    • 2019-08-31
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    相关资源
    最近更新 更多