# How to count the number of occurrences of a particular text inside a table in a page in Selenium with python?

Selenium Web DriverAutomation TestingSoftware Testing

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()
Published on 29-Jul-2020 14:18:00