【发布时间】: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