0

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

Bee-kay
  • 3
  • 2
  • Update the question with the relevant HTML of the element. – undetected Selenium Dec 19 '21 at 18:11
  • Thanks for the response @DbjB, let me work through your examples. – Bee-kay Dec 20 '21 at 14:18
  • What is the website and specific table you wish to target? The html compound class you have shown is pretty common [Bootstrap](https://getbootstrap.com/docs/4.0/content/tables/) and often used more than once on a page. – QHarr Dec 21 '21 at 10:39
  • I'm beginning to suspect that the issue is libraries based. Is there a chance that excel 2010 does not support usage of compound names at all? – Bee-kay Dec 21 '21 at 19:40
  • Responding to QHarr: this compound name is listed only once and is, painstakingly, the only element that holds the targeted table. Let me upload more info. – Bee-kay Dec 21 '21 at 20:33
  • See if there is one class from those values which can be used to uniquely identify the table e.g. `FindElementByCss(".table-striped")` . Otherwise, replace the spaces with "." in the css selector. – QHarr Dec 22 '21 at 06:12

1 Answers1

0

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
Elio Fernandes
  • 1,335
  • 2
  • 14
  • 29
  • Elio, all examples return an error '32' msg. I already am suspicious of a libraries issue, i.e. I'm using excel 2010, instead of the current version. The .driver reference seems to not be supported at all. I'll upload the website and the specific page. – Bee-kay Dec 21 '21 at 21:07
  • I have tried the code I posted and all the examples works! Can you share the url that you're using? Share the entire error message too. – Elio Fernandes Dec 21 '21 at 22:08
  • That's true, Elio, hence I'm of the opinion that: the code works i.e. on the excel 2016 platform and not on the 2010 version. If I may, are you by any chance running this test on a 2016 platform? Let me upload as requested. – Bee-kay Dec 22 '21 at 13:28
  • I have Excel 2013, 2016 and Office 365. – Elio Fernandes Dec 22 '21 at 15:07
  • Can we have the code tested on the 2010 platform and see if output will be positive. I already have updated the question as per request. Thanks. – Bee-kay Dec 22 '21 at 19:18
  • Boom! Like Magic @Elio, big-ups man. A MILLION votes for patience. Thanks once more. – Bee-kay Dec 23 '21 at 13:40