How To Handle Web Tables in Selenium Python
In the previous articles on Selenium Python Tutorial, we have covered “Exceptions in Selenium Python“. In this tutorial, we will learn How To Handle Web Tables in Selenium Python.Â
A web table in an HTML document is defined under <table> tag. The rows of a table are represented with the <tr> tag and the columns in a table are represented with the <td> tag. Mostly each table has headers represented with the <th> tag.
Let us consider the below table for our discussion.
Check this link for the below table.Â
To work with a web table, we should be able to handle scenarios like how to fetch the row numbers, column numbers, a particular cell value, fetch all cell values in a row, fetch all cell values in a column, fetch all the cell values and so on.
To calculate the total number of rows, we shall first create a customized xpath to represent all rows in a table with the help of find_elements_by_xpath () method. Since this method returns a list, we can obtain the row count with the help of len method.
Code Implementation to calculate row count in a table.
# import the webdriver from selenium import webdriver # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get("https://www.softwaretestingmaterial.com/sample-webpage- to-automate/") # to identify the table rows l = driver.find_elements_by_xpath ("//*[@class= 'spTable']/tbody/tr") # to get the row count len method print (len(l)) # to close the browser driver.quit ()
To calculate the total number of columns, we shall first create a customized XPath to represent all rows in a table with the help of find_elements_by_xpath () method. Since this method returns a list, we can obtain the row count with the help of len method. We have to fix a row number before calculating the column count with <td>. If we set row 1, we have to use <th> for the columns. For any other row except 1, we have to use <td>.
Code Implementation to calculate column count in a table.
# import the webdriver from selenium import webdriver # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get("https://www.softwaretestingmaterial.com/sample-webpage- to-automate/") # to identify the columns in row 3 l=driver.find_elements_by_xpath("//*[@class= 'spTable']/tbody/tr[3]/td") # to get the column count len method print (len(l)) # to close the browser driver.quit ()
To get the headers in a table, we shall first create a customized XPath to represent all rows in a table with the help of find_elements_by_xpath () method. Since this method returns a list, we can obtain the row count with the help of len method. We have to iterate through row number 1, then move to <th>. Once we get hold of the first row, we shall use the text method to get all the table headers.
Code Implementation to get the table headers.
# import the webdriver from selenium import webdriver # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get("https://www.softwaretestingmaterial.com/sample-webpage- to-automate/") # to identify the table column headers in row 1 l=driver.find_elements_by_xpath ("//*[@class= 'spTable']/tbody/tr[1]/th") # to traverse through the list of headers for i in l : # to get the header with text method print (i.text) #to close the browser driver.close ()
To get all the cell values of a particular row, we shall first create a customized XPath to represent all cell data of a row in a table with the help of find_elements_by_xpath () method. Since this method returns a list, we can obtain the row count with the help of len method. We have to iterate through the columns of a particular row number [greater than 1], having <td> tag. Once we get hold of all the values in that particular row, we shall use the text method to get all the cell data.
Code Implementation to get cell data of a particular row.
# import the webdriver from selenium import webdriver # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get("https://www.softwaretestingmaterial.com/sample-webpage- to-automate/") # to identify the table column l=driver.find_elements_by_xpath ("//*[@class= 'spTable']/tbody/tr[3]/td") # to traverse through the list of cell data of row 3 for i in l : # to get the cell data with text method print (i.text) #to close the browser driver.close ()
To get all the cell values of a particular column, we shall first create a customized XPath to represent all cell data of a column in a table with the help of find_elements_by_xpath () method. Since this method returns a list, we can obtain the row count with the help of len method. We have to iterate through the rows of a particular column number, having a <tr> tag. Once we get hold of all the values in that particular column, we shall use the text method to get all the cell data.
Code Implementation to get cell data of a particular column.
# import the webdriver from selenium import webdriver # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get("https://www.softwaretestingmaterial.com/sample-webpage- to-automate/") # to identify the table column l=driver.find_elements_by_xpath ("//*[@class= 'spTable']/tbody/tr/td[3]") # to traverse through the list of cell data of column 3 for i in l : # to get the cell data with text method print (i.text) #to close the browser driver.close ()
To get all the cell values of a table, we shall first create a customized XPath to represent all the rows and columns in a table with the help of find_elements_by_xpath () method. Since this method returns a list, we can obtain the row and column count with the help of len method. We have to iterate through each row and each column of a particular table then fetch the cell data with the text method.
Code Implementation to get all the cell data in a table.
# import the webdriver from selenium import webdriver # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL u="https://www.softwaretestingmaterial.com/sample-webpage-to-automate/" driver. get (u) # to identify the table rows r = driver.find_elements_by_xpath ("//table[@class= 'spTable']/tbody/tr") # to identify table columns c = driver.find_elements_by_xpath ("//*[@class= 'spTable']/tbody/tr[3]/td") # to get row count with len method rc = len (r) # to get column count with len method cc = len (c) # to traverse through the table rows excluding headers for i in range (2, rc + 1) : # to traverse through the table column for j in range (1, cc + 1) : # to get all the cell data with text method d = driver.find_element_by_xpath ("//tr["+str(i)+"]/td["+str(j)+"]").text print (d) #to close the browser driver.close ()
To verify if a particular text exists in the table, we shall first create a customized xpath to represent all the rows and columns in a table with the help of find_elements_by_xpath () method. Since this method returns a list, we can obtain the row and column count with the help of len method.
We have to iterate through each row and each column of a particular table then fetch the cell data with the text method. Once the cell data is fetched, we shall verify if it matches with the text we are looking for with the help of text () function in xpath.
Code Implementation to search a specific text in a table.
# import the webdriver from selenium import webdriver # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL u="https://www.softwaretestingmaterial.com/sample-webpage-to-automate/" driver. get (u) # to identify the table rows r = driver.find_elements_by_xpath ("//table[@class= 'spTable']/tbody/tr") # to identify table columns c = driver.find_elements_by_xpath ("//*[@class= 'spTable']/tbody/tr[3]/td") # to get row count with len method rc = len (r) # to get column count with len method cc = len (c) # to traverse through the table rows excluding headers for i in range (2, rc + 1) : # to traverse through the table column for j in range (1, cc + 1) : # to get all the cell data with text method d = driver.find_element_by_xpath ("//tr["+str(i)+"]/td["+str(j)+"]").text # to search for our required text m = driver.find_elements_by_xpath ("//td[text() = 'EID001']") # to get the size of the list with len method s = len (m) # check if list size greater than 0 If (s > 0) : print ("Text found") #to close the browser driver.close ()
To get value from a specific cell in a table, we shall first create a customized XPath to identify the cell with the help of find_element_by_xpath () method
Code Implementation to get the value from a particular cell.
# import the webdriver from selenium import webdriver # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL u="https://www.softwaretestingmaterial.com/sample-webpage-to-automate/" driver. get (u) # to identify a cell , row 3 and column 2 c = driver.find_element_by_xpath ("//*[@class= 'spTable']/tbody/tr[3]/td[2]") # to get the text print (c.text) #to close the browser driver.close ()
In the next article, we will learn Handling cookies in Selenium Python
Related posts:
- Selenium Python Tutorial
- Selenium Java Tutorial
- Selenium Interview Questions
- API Testing Tutorial
- Postman Tutorial