Selenium VBA Extracting All Tr from a Table ID - Chrome x IE

tiago_rbs

New Member
Joined
Feb 12, 2022
Messages
5
Office Version
  1. 2010
Platform
  1. Windows
I'm migrating an old spreadsheet that I use to collect/send information on a website, originally made in IE, I'm migrating to Selenium (Chrome).

I need help with this part. I need to get all tags: "tr" in a table id: "tablename":

In IE it works perfectly:

VBA Code:
    Sub InternetExplore()
    
        Dim objIE As InternetExplorer
        Dim ele As Object
        Dim y As Integer
        
        objIE.Visible = True
        objIE.navigate "URL"
        Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
     
        y = 1
    
        For Each ele In objIE.document.getElementById("tablename").getElementsByTagName("tr")
    
            Sheets("Sheet1").Range("A" & y).Value = ele.Children(0).textContent
            Sheets("Sheet1").Range("B" & y).Value = ele.Children(1).textContent
            y = y + 1
    
        Next
    
    
    End Sub


I need help to migrate this code for Selenium (VBA), i tried this, but it doesn't work.

Appear a error code 438: "Object Doesn’t Support This Property or Method" in the first line into the for :

"Sheets("Sheet1").Range("A" & y).Value = ele.Children(0).textContent"

VBA Code:
   Sub Chrome()
    
        Dim ele As Object
        Dim y As Integer
        
        Set objIE = New Selenium.ChromeDriver
        objIE.Start
        objIE.Get "URL"
     
         y = 1
    
         For Each ele In objIE.FindElementById("tablename").FindElementsByTag("tr")
            Sheets("Sheet1").Range("A" & y).Value = ele.Children(0).textContent        
            Sheets("Sheet1").Range("B" & y).Value = ele.Children(1).textContent   
            y = y + 1
        Next
    
    
    End Sub



Could anyone help me please?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Selenium objects don't have the children property, hence the error.

The children of tr elements are th (table headers) or td (table data) so try this:

VBA Code:
            Sheets("Sheet1").Range("A" & Y).Value = ele.FindElementsByTag("td")(0).text
            Sheets("Sheet1").Range("B" & Y).Value = ele.FindElementsByTag("td")(1).text
 
Upvote 0
Selenium objects don't have the children property, hence the error.

The children of tr elements are th (table headers) or td (table data) so try this:

VBA Code:
            Sheets("Sheet1").Range("A" & Y).Value = ele.FindElementsByTag("td")(0).text
            Sheets("Sheet1").Range("B" & Y).Value = ele.FindElementsByTag("td")(1).text
@John_w

Hello! Thank you!

I tried this, but appear the error:

"Index was outside the bounds of the array"

If i try this:

VBA Code:
Sheets("Sheet1").Range("A" & y).Value = ele.Text


Write in ("A" & y) all table content but it joins the 4 columns of the table in 1 column, example:

A1 = "table column 1 " & "table column 2 " & "table column 3 "

But I need to separate it, i need each column in one cell.

Is there a way to do this in Selenium? All tag is "Td", and look for path or class i think is not an option because i don't know how many elements will be in table, the only thing i know is there is a lot of elements and all tags is "Td"
 
Upvote 0
Sorry, I forgot that in Selenium array bounds start at 1, not 0. Therefore change the lines to (1) and (2).
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,191
Members
452,616
Latest member
intern444

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