
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
Count Occurrences of Text in Table with Selenium and Python
We can count the number of occurrences of a particular text inside a table in Selenium. First of all we need to locate the element by xpath. In xpath, we have a particular text() function that identifies elements based on the visible text on the screen.
Then we have to use find_elements method to get the list of matching elements having the text we are looking for on the page. Finally we need to get the size of that list with the help of the len function of the list.
This will give the number of occurrences of a particular text inside a table.
Syntax
driver.find_elements_by_xpath("//td[text()='Tutorialspoint']")
Example
Coding Implementation to get the count of occurrences of a text inside 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() # identifying the text keyword inside the table dta = driver.find_elements_by_xpath("//td[text()='keyword']") # len method is used to get the count of occurrences print(len(dta)) #to close the browser driver.close()
Advertisements