Skip to content
Related Articles

Related Articles

Python Selenium – Find Button by text

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 03 Mar, 2021
Improve Article
Save Article

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-

Firefoxhttps://github.com/mozilla/geckodriver/releases
Safarihttps://webkit.org/blog/6900/webdriver-support-in-safari-10/
Chromehttps://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:


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!