Getting inner text of table VBA/Selenium

noctash

New Member
Joined
Aug 4, 2022
Messages
16
Office Version
  1. 365
Platform
  1. Windows
I am trying to copy the values of table cells on a web page to excel. The table has a variable number of rows, but I only need the first two columns.

Currently, I am using the following code, but it is taking a LONG time, a second or two for each row, which can take a long time for a big table.
Is there a faster way to do this that will copy over only the first two columns?

VBA Code:
Dim Driver As New Selenium.ChromeDriver
Set Driver = CreateObject("Selenium.ChromeDriver")

Driver.Timeouts.ImplicitWait = 1

Driver.Start baseUrl:="https://website.com"
Driver.get "/table"
Dim Table As Selenium.WebElement
Set Table = Driver.FindElementById("tableID")

    Dim AllRows As Selenium.WebElements
    Dim SingleRow As Selenium.WebElement
    Dim AllRowCells As Selenium.WebElements
    Dim SingleCell As Selenium.WebElement
    Dim OutputSheet As Worksheet
    Dim RowNum As Long, ColNum As Long
    Dim TargetCell As Range
    Set OutputSheet = ThisWorkbook.Worksheets("REFERENCE")

    Set AllRows = Table.findelementsbytag("tr")
        For Each SingleRow In AllRows
            RowNum = RowNum + 1
            Set AllRowCells = SingleRow.findelementsbytag("td")

            If AllRowCells.Count = 0 Then
                Set AllRowCells = SingleRow.findelementsbytag("th")
            End If

            For Each SingleCell In AllRowCells
                ColNum = ColNum + 1
                 Set TargetCell = OutputSheet.Cells(RowNum, ColNum)
                 TargetCell.Value = SingleCell.Text
  
            Next SingleCell
                ColNum = 0
        Next SingleRow
 
Last edited:

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
I've never seen this before, but I did a quick Google to see about it and came up with this next sentence. I'm wondering if you change your 1 for 0, if it will make any difference. Regardless of that, I wish you all the best. 🙏

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements (if) they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top