Open In App

Python Selenium – Find element by text

Last Updated : 03 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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’)]”)

Let’s understand this with implementation:

Step 1: First, import the libraries, selenium, and time.

Python




from selenium import webdriver
from 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 webdriver
from selenium import webdriver
  
#import time
from time import sleep
  
# create webdriver object
driver = webdriver.Chrome(
    executable_path="C:\selenium\chromedriver_win32\chromedriver.exe")
  
# get the website
  
# sleep for some time
sleep(3)
  
# get element through text
driver.find_element_by_xpath("// a[contains(text(),\
'5 CHEAP HOLIDAY')]").click()
  
# sleep for some time
sleep(4)


Output:

Further, the text to be found is searched and displays the output as given below.

More locators for locating single elements:

Locators

Description

find_element_by_id  The first element with the id attribute value matching the location will be returned.
find_element_by_name  The first element with the name attribute value matching the location will be returned.
find_element_by_xpath  The first element with the xpath syntax matching the location will be returned.
find_element_by_link_text  The first element with the link text value matching the location will be returned.
find_element_by_partial_link_text  The first element with the partial link text value matching the location will be returned.
find_element_by_tag_name The first element with the given tag name will be returned.
find_element_by_class_name  The first element with the matching class attribute name will be returned.
find_element_by_css_selector  The first element with the matching CSS selector will be returned.


Previous Article
Next Article

Similar Reads

text element method - Selenium Python
Selenium’s Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. To open a webpage using Selenium Python, checkout - Navigating links using get method – Selenium Python. Just being able to go to places isn’t terribly useful. What
2 min read
Python Selenium - Find Button by text
In this article, let's discuss how to find a button by text using selenium. See the below example to get an idea about the meaning of the finding button by text. Example: URL: https://html.com/tags/button/ We need to find the "CLICK ME!" button using the text "Click me!". Module Needed: Selenium: The selenium package is used to automate web browser
2 min read
Get all text of the page using Selenium in Python
As we know Selenium is an automation tool through which we can automate browsers by writing some lines of code. It is compatible with all browsers, Operating systems, and also its program can be written in any programming language such as Python, Java, and many more. Selenium provides a convenient API to access Selenium WebDrivers like Firefox, IE,
3 min read
Text Searching in Google using Selenium in Python
Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python. In this article, we are going to see how to automate our browser. We can jus
3 min read
Click button by text using Python and Selenium
Selenium is a tool that provides APIs to automate a web application to aid in its testing. In this article, we discuss the use of Selenium Python API bindings to access the Selenium WebDrivers to click a button by text present in the button. In the following example, we take the help of Chrome. The method used is the find_element_by_link_text() whi
2 min read
How to get text of a tag in selenium - Python?
Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python. In this article, we will write a Python Script for getting the text from the
1 min read
How to simulate pressing enter in HTML text input with Selenium ?
Selenium is an inbuilt module available in python that allows users to make automated suites and tests. We can build code or scripts to perform tasks automatically in a web browser using selenium. Selenium is used to test the software by automation. Also, programmers can create automated test cases for the software or app using the selenium. By rea
3 min read
Selenium - Search for text on page
Prerequisite:- Selenium Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python. In this article, we will learn how to know whether t
1 min read
is_selected() element method - Selenium Python
Selenium’s Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. To open a webpage using Selenium Python, checkout - Navigating links using get method – Selenium Python. Just being able to go to places isn’t terribly useful. What
2 min read
is_displayed() element method - Selenium Python
Selenium’s Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. To open a webpage using Selenium Python, checkout - Navigating links using get method – Selenium Python. Just being able to go to places isn’t terribly useful. What
2 min read
three90RightbarBannerImg