Counting the number of rows(tr) in a table(html) in a website using selenium in VBA

Gautamsunil

New Member
Joined
Apr 19, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi all
I am extracting data of individual student from an institute's website into excel spreadsheet for further manipulation. Though the number of questions in the website are same for all, number of answers differ from one student to another depending upon the number questions he answered. Because of this reason, for the same topic, one student has 5 rows in the table and the other has 7 rows in the same table. I am guessing counting the number of rows in that particular table in the website and then applying if else condition will solve the issue. How can I count the number of rows in a table in a website?
I have tried few codes I found around but ran into problem every single time.
I am using selenium basic to allow extraction through chrome. I have done same thing through Internet Explorer without having any issues.
Any help would be much appreciated as I have spent hours trying to solve this.
Thanks in advance.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,808
Try this:
VBA Code:
    Dim theTable As WebElement
    '...... your code to get the table
    Debug.Print theTable.FindElementsByTag("TR").Count
 
Solution

Gautamsunil

New Member
Joined
Apr 19, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Try this:
VBA Code:
    Dim theTable As WebElement
    '...... your code to get the table
    Debug.Print theTable.FindElementsByTag("TR").Count
Thanks a lot. This was exactly what I needed.
 

Marc L

Well-known Member
Joined
Apr 5, 2021
Messages
902
Office Version
  1. 2010
Platform
  1. Windows
John,​
as a web table as a rows collection so you can directly extract {table object}.rows.length …​
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,808

ADVERTISEMENT

Marc, the rows property isn't available for me. These are the only properties available:
1620144708028.png
 

Marc L

Well-known Member
Joined
Apr 5, 2021
Messages
902
Office Version
  1. 2010
Platform
  1. Windows

Ok I was thinking of DOM under IE but it not exists under selenium, so weird …​
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,808
Yes, it's odd that the Selenium library has only one generic class (WebElement), whereas the MS HTML library has a specific class for many HTML elements.
 

Watch MrExcel Video

Forum statistics

Threads
1,132,640
Messages
5,654,541
Members
418,139
Latest member
nimesh72

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
Top