Selenium VBA click on item in a table (do other stuff) and proceed to next item in the table,do until empty

adinev

New Member
Joined
Nov 10, 2022
Messages
12
Office Version
  1. 365
Platform
  1. Windows
New to browser automation here. I am trying to open a webpage and browse a list of entries. The bot has to open the first one , execute some tasks ,once done proceed to the next entry until the end. Can someone with exp. give some hints. Thanks a ton.

I get a "object doesnt support this method" error at "For Each tblent In tbl"

Cant share the full html due to corporate security Thank you

VBA Code:
Option Explicit
Public Sub xyz_Bot()

Dim pause1 As String
 
 pause1 = "00:00:10"

Dim pause2 As String
 
 pause2 = "00:00:30"

Dim url As String
 
 url = "http://xyz/"

Dim driver As New webdriver

Dim rowc, cc, columnC As Integer

Dim tbl As Object

Dim tblent As Object


''''''''''''''''''''''''''''
'Log in to xyz
''''''''''''''''''''''''''''

driver.Start "Chrome"

driver.Window.Maximize

driver.Get url

Application.Wait Now + TimeValue(pause1)


''''''''''''''''''''''''''''
'Find abc
''''''''''''''''''''''''''''

driver.FindElementByXPath("/html/body/div[2]/div[1]/ul/li[5]/ul/li[4]/a/span").Click

Application.Wait Now + TimeValue(pause1)

''''''''''''''''''''''''''''
'Filter bca
''''''''''''''''''''''''''''

driver.FindElementByXPath("/html/body/div[2]/div[2]/div[2]/div[2]/div/div/div[1]/div[2]/div/div[2]/div[3]/div[2]/select").AsSelect.SelectByText ("Renewal")

driver.FindElementByXPath("/html/body/div[2]/div[2]/div[2]/div[2]/div/div/div[1]/div[2]/div/div[1]/div[5]/button[1]").Click


Set tbl = driver.FindElementByXPath("/html/body/div[2]/div[2]/div[2]/div[2]/div/div/div[2]/div[1]/table")

 For Each tblent In tbl

Debug.Print tblent.Text 'here the bot is going to do stuff 


 Next tblent










End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
For a HTML table, loop through the table's rows (tr elements) and headers (th elements) or data cells (td elements) in each row:
VBA Code:
    Dim tRow As WebElement
    Dim tCell As WebElement
        
    For Each tRow In tbl.FindElementsByTag("tr")
        For Each tCell In tRow.FindElementsByTag("td")
            Debug.Print tCell.Text
        Next
    Next
 
Upvote 0
Hi ,
i still get the error "Object does not support property of method"
 
Upvote 0
This works for me.
VBA Code:
Public Sub Extract_HTML_Table()

    Dim driver As Selenium.EdgeDriver
    Dim table As WebElement
    Dim tRow As WebElement
    Dim tCell As WebElement
    
    Set driver = New Selenium.EdgeDriver    
    With driver
        .Start
        .Get "https://www.w3schools.com/html/html_tables.asp"
    End With
    
    '<table class="ws-table-all" id="customers">

    Set table = driver.FindElementById("customers")    
    For Each tRow In table.FindElementsByTag("tr")
        Debug.Print "Row index " & tRow.Attribute("rowIndex")
        For Each tCell In tRow.FindElementsByTag("td")
            Debug.Print tCell.Attribute("cellIndex"), tCell.Text
        Next
    Next
    
    Range("A1").CurrentRegion.Clear
    table.AsTable.ToExcel Range("A1")
    
End Sub
 
Upvote 0
Dim table As WebElement
Dim tRow As WebElement
Dim tCell As WebElement
Set table = driver.FindElementByTag("tbody")




Set tRow = driver.FindElementByTag("tr")

For Each tRow In table ''''' here I get the error "Object does not support property or method"
tRow.Click
Application.Wait Now + TimeValue(pause1)

Next tRow
 
Upvote 0
Hi John,
it works now . But i get a "Stale element reference" when going for the second iteration of the loop. Do you have any idea how to fix it?
 
Upvote 0
Not without accessing the web page. If you expect the table to contain 10 rows you could do this:

VBA Code:
    Dim tRows As WebElements, tRow As WebElement
    Set tRows = table.FindElementsByTag("tr", 10, 5000)
    For Each tRow In tRows
        Debug.Print tRow.Text
    Next
 
Upvote 0
Not without accessing the web page. If you expect the table to contain 10 rows you could do this:

VBA Code:
    Dim tRows As WebElements, tRow As WebElement
    Set tRows = table.FindElementsByTag("tr", 10, 5000)
    For Each tRow In tRows
        Debug.Print tRow.Text
    Next
Hi John, I wanted to thank you. The loop works just fine. I solved the stale element error by declaring the elements again before the loop goes for the second row.
 
Upvote 0

Forum statistics

Threads
1,223,882
Messages
6,175,164
Members
452,615
Latest member
bogeys2birdies

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