Open In App

Python Selenium – Find Button by text

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

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 interaction from Python. It is an open-source tool primarily used for testing. Run the following command in the terminal to install this library:

pip install selenium

Setup Web Drivers:

Web Driver is a package to interact with a Web Browser. You can install any Web Driver according to your browser choice. Install any one of them using the given links-

Firefox https://github.com/mozilla/geckodriver/releases
Safari https://webkit.org/blog/6900/webdriver-support-in-safari-10/
Chrome https://sites.google.com/a/chromium.org/chromedriver/downloads

Here, we are going to use ChromeDriver.

Find xpath of the button:

  • Method 1: Using Inspect Element
    Right Click on the element you are trying to find the xpath. Select the “Inspect” option.
     

  • Right click on the highlighted area on the console. Go to Copy xpath
     

  • Method 2: Using Chrome Extension to find xpath easily: 
    We can easily find xpath of an element using a Chrome extension like SelectorGadget.
     

Approach:

  • Import Selenium and time library
  • Set the Web Driver path with the location where you have downloaded the WebDriver
    Example- “C:\\chromedriver.exe”
  • Call driver.get() function to navigate to a particular URL.
  • Call time.sleep() function to wait for the driver to completely load the webpage.
  • Use driver.find_element_by_xpath() method to find the button using xpath.
  • Finding button by text-
    (i) Using normalize-space() method:
    driver.find_element_by_xpath(‘//button[normalize-space()=”Click me!”]’)
    (ii) Using text() method:
    driver.find_element_by_xpath(‘//button’)
    Note: It is recommended to use normalize-space() method because it trim the left and right side spaces. It is possible that there can be spaces present at the start or at the end of the target text.
  • Lastly close the driver using driver.close() function.

Implementation:

Python3




# Import Library
from selenium import webdriver
import time
  
# set webdriver path here it may vary
# Its the location where you have downloaded the ChromeDriver
driver = webdriver.Chrome(executable_path=r"C:\\chromedriver.exe")
  
# Get the target URL
  
# Wait for 5 seconds to load the webpage completely
time.sleep(5)
  
# Find the button using text
driver.find_element_by_xpath('//button[normalize-space()="Click me!"]').click()
  
time.sleep(5)
  
# Close the driver
driver.close()


Output:



Previous Article
Next Article

Similar Reads

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
Python Selenium - Find element by text
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 aut
3 min read
How to Set Text of Tkinter Text Widget With a Button?
Prerequisite: Python GUI – tkinter Text Widget is used where a user wants to insert multi-line text fields. In this article, we are going to learn the approaches to set the text inside the text fields of the text widget with the help of a button. Approach: Using insert and delete methodImport the Tkinter module.Create a GUI window.Create our text w
2 min read
How to click a button on webpage using Selenium?
This article is all about how to click any button using Selenium on a webpage and many more concepts related to the same which are discussed below. Table of Content What is Selenium?How to click on a button using SeleniumConclusionFrequently Asked Questions on How to click a button on webpage using selenium?What is Selenium?Selenium is a widely use
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 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
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
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
Practice Tags :