
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get All Values Including Headers in a Table Using Selenium with Python
We can get all the values inside a table in Selenium with the help of find_elements method. The rows of a table are represented by <tr> tag in html code. To get all the rows, we shall use the locator xpath and then use find_elements_by_xpath method. The list of rows will be returned. Next we need to compute the size of the list with the help of len method.
Syntax
driver.find_elements_by_xpath("//table/tbody/tr")
Once we get all the rows, we now have to compute the number of columns.
The headers of a table are represented by <th> tag in html and always in the first row of the table. The rows are identified with <tr> tag in html. The total count of the number of column headers is mostly equal to the total number of columns. A <th> tag’s parent is always a <tr> tag.
The logic is to get all the headers. We shall use the locator xpath and then use find_elements_by_xpath method. The list of headers will be returned. Next we need to compute the size of the list with the help of len method.
Syntax
driver.find_elements_by_xpath("//table/tbody/tr[1]/th")
The html code snippet of a table column count is as described below −
Example
Coding Implementation to get all the values in a table.
from selenium import webdriver #browser exposes an executable file #Through Selenium test we will invoke the executable file which will then #invoke actual browser driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") # to maximize the browser window driver.maximize_window() #get method to launch the URL driver.get("https://www.tutorialspoint.com/plsql/plsql_basic_syntax.htm") #to refresh the browser driver.refresh() # to get the row count of table rws = driver.find_elements_by_xpath("//table/tbody/tr") # len method is used to get the size of that list r = len(rws) # to get column count of table cols = driver.find_elements_by_xpath("//table/tbody/tr[1]/th") # len method is used to get the size of that list c = len(cols) el m,nemt = [] #iterate over the rows for i in range(1,r): # row data set to 0 each time in list row = [] #iterate over the columns for j in range(1,c): # getting text from the ith row and jth column d=driver.find_element_by_xpath("//tr["+str(i)+"]/td["+str(j)+"]").text row.append(d) #finally store and print the list in console elemt.append(row) print(elemt) #to close the browser driver.close()