Python Selenium – Find element by text

03 Mar, 2021
The technique to verify if the requirements given by the user meet with the actual software product developed is known as Software Testing. Moreover, it also checks if the final software product developed is error-free or not. Software testing can either be performed manually or with the help of software testing tools. The testing which is done automatically through the inbuilt tools is known as Automation Testing. An incredible library in Python, Selenium helps you in automated testing of the software. While doing automation testing, are you not able to find an element by text in the required web page? Then, you are at an appropriate place. In this article, we will be discussing the appropriate steps along with an example to do the same.
Syntax: driver.find_element_by_xpath(“//tag [contains( text(), ‘word’)]”)
Parameters:
tag: Here, tag stands for the tag name which contains the particular word.
word: Here, word stands for the text which has to be found in a particular string. We don’t need to write a complete sentence which we want to find, but we can write a few words which are unique in the web page.
Example: For instance, consider this simple page source:
HTML
<!DOCTYPE html><html> <body> <button type= “button” >Geeks For Geeks</button> </body> <html>
Once you have created a driver, you can grasp an element using:
button = driver.find_element_by_xpath (“//button[contains( text( ), ‘Geeks for Geeks’)]”)
Step 1: First, import the libraries, selenium, and time.
Python
from selenium import webdriverfrom time import sleep
Step 3: Next, establish a connection with the web driver through the executable path.
Python3
driver = webdriver.Chrome(    executable_path="#path where you have installed webdriver")
Step 4: Now, obtain the website in which you want to find the element.
Python3
driver.get("#Enter the website URL")
Step 5: Then, make python sleep for few seconds so, by that time, the webpage gets loaded.
Python3
sleep(#duration)
Step 6: Finally, find the desired element through text on the given web page.
Python3
driver.find_element_by_xpath(“//#tag name which contains word [contains( text(),                             ‘#word to be found in a particular string’)]”)
Below is the full implementation:
Python
# Python Program to find element by text  #import webdriverfrom selenium import webdriver  #import timefrom time import sleep  # create webdriver objectdriver = webdriver.Chrome(    executable_path="C:\selenium\chromedriver_win32\chromedriver.exe")  # get the websitedriver.get("http://bit.ly/vinayakgfg")  # sleep for some timesleep(3)  # get element through textdriver.find_element_by_xpath("// a[contains(text(),\'5 CHEAP HOLIDAY')]").click()  # sleep for some timesleep(4)
Output:
Further, the text to be found is searched and displays the output as given below.
Picked
Python Selenium-Exercises
Python-selenium
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
How to drop one or multiple columns in Pandas Dataframe
Python | os.path.join() method
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python | Get unique values from a list
Python | datetime.timedelta() function