在 selenium 中通过...(."<table class = "table table-bordered table-condensed table-striped text-center table-hover">") 查找元素

Find element by...(."<table class = "table table-bordered table-condensed table-striped text-center table-hover">") in selenium

我正在使用 Selenium.Driver 查找 div class 元素,该元素应该 return 网页中的特定 table。 虽然通过按标签查找方法成功抽象了整个页面,但我现在面临的挑战是,return 只有页面内的 table,除了 table class被列为“化合物名称”并且在 Selenium 中不受支持: 我已经尝试了 .xpath 和 .css 方法,但都没有成功。我的失败可能是因为使用了错误的表达方式。

我的代码:

Set HTMLTables = HTMLDoc.FindElementsByTag("table")

' The above code returns all elements within the entire page.

' Instead of finding elements by "table" tag,
' I wanna FindElement(s)By...("table table-bordered table-condensed table-striped 
  text-center table-hover")
' The given code shall return ONLY the TABLE from within the entire page.

这是我的问题 update,我添加了 micro 和目标 html pageurl link 也发布了。

code: enter image description here

url link: https://portalseven.com/lottery/southafrica_powerball_winning_numbers.jsp?viewType=2&timeRange=3

如果您正在寻找所有化合物 class 选择这个

Dim Table As Selenium.WebElement
Set Table = driver.FindElementByXPath("//*[@class='table table-bordered table-condensed table- striped text-center table-hover']")

如果您正在寻找化合物的一部分 class 您也可以使用

Dim FindBy As New Selenium.By
If Not driver.IsElementPresent(FindBy.Class("table-condensed"), 3000) Then
    driver.Quit
    Exit Sub
Else
    ' do something ...
    Set Table = driver.FindElement(FindBy.Class("table-condensed"))
End If

或者

Dim FindBy As New Selenium.By
If Not driver.IsElementPresent(FindBy.Css(".table-bordered"), 3000) Then
    driver.Quit
    Exit Sub
Else
    ' do something ...
    Set Table = driver.FindElement(FindBy.Css(".table-bordered"))
End If

您的代码的问题在 Set Table = ... 中。将下面的 Set Table 行与您的进行比较。 我在 Excel 2007 测试了这个程序,它有效!

Sub Selenium_FindElementByClass_Compound()
    Dim driver As New WebDriver
    Dim myUrl As String
    Dim Table As Selenium.WebElement
       
    ' Set URL
    myUrl = "https://portalseven.com/lottery/southafrica_powerball_winning_numbers.jsp?viewType=2&timeRange=3"
    
    ' Open chrome
    driver.Start "Chrome"
    
    ' Navigate to Url
    driver.Get myUrl
    Application.Wait Now + TimeValue("00:00:5")
    
    ' Find table
    Set Table = driver.FindElementByXPath("//*[@class='table table-bordered table-condensed table-striped text-center table-hover']")
    
    ' Copy table to Excel
    Table.AsTable.ToExcel ThisWorkbook.Worksheets.Add.Range("A1")
End Sub