
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 Data from Specific Cell in Selenium with Python
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()