How to get all the values including the headers inside a table in a page in 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()

Advertisements