# How to get the data from a specific cell value (say 2nd row and 2nd column) inside a table in Selenium with python?

Selenium Web DriverAutomation TestingSoftware Testing

We can extract values from a specific cell (say 2nd row and 3rd column) inside a table in Selenium. First of all we need to locate the cell with the help of xpath locator.

Since the row and column numbers are given, we can create a customized xpath with the help of indexes specified for both <tr> and <td> tags. The rows of a table are represented by <tr> tag in html code. The data in each row is enclosed with the <td> tag in html. Thus a <td> tag’s parent is always a <tr> tag.

So to get the value at the second row and second column, we have to mention the row as tr[2] and the third column will be identified as td[2].

## Syntax

driver.find_element_by_xpath("//table/tbody/tr[2]/td[2]")

The html code snippet of a table header is as described below −

## Example

Coding Implementation for getting value from second row and second column.

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 data from 2nd row and 2nd column directly
val = driver.find_elements_by_xpath("//table/tbody/tr[2]/td[2]").text
# print the value in console
print(val)
#to close the browser
driver.close()
Published on 29-Jul-2020 10:49:25