Thursday, October 2, 2025
HomeLanguagesPython Selenium – Find Button by text

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 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:

RELATED ARTICLES

Most Popular

Dominic
32331 POSTS0 COMMENTS
Milvus
85 POSTS0 COMMENTS
Nango Kala
6703 POSTS0 COMMENTS
Nicole Veronica
11867 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11927 POSTS0 COMMENTS
Shaida Kate Naidoo
6818 POSTS0 COMMENTS
Ted Musemwa
7080 POSTS0 COMMENTS
Thapelo Manthata
6775 POSTS0 COMMENTS
Umr Jansen
6776 POSTS0 COMMENTS