I'm using Selenium.Driver to find a div class element that should return a specific table within a webpage. Whilst having succeeded in abstracting the entire page through the find by tag method, my challenge is to now, return ONLY the table within the page, except that the table class is listed as a "compound name" and not supported in Selenium: I've tried both the .xpath and the .css methods without success. My failure could be as a result of using wrong expressions.

My code:

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.

Here's an update of my question, I've added both the micro and the targeted html page. The url link is also posted.

code: enter image description here

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

1

There are 1 best solutions below

6
Elio Fernandes On BEST ANSWER

If you are looking for all the compound class choose this

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

If you are looking for part of the compound class you can also use

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

Or

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

The problem on your code is in Set Table = .... Compare this Set Table line below with yours. I tested this procedure in Excel 2007 and it works!

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