Python Selenium - Find Button by text Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like 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- Firefoxhttps://github.com/mozilla/geckodriver/releasesSafarihttps://webkit.org/blog/6900/webdriver-support-in-safari-10/Chromehttps://accounts.google.com/v3/signin/identifier?continue=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fsites.google.com%2Fa%2Fchromium.org%2Fchromedriver%2Fdownloads&followup=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fsites.google.com%2Fa%2Fchromium.org%2Fchromedriver%2Fdownloads&ifkv=AdBytiO-mLfl-8FJrPj_o6lJSZZRSges1SVhCiFmgni5_Zm4w-MzBlTuDqpgM5V_SwU3xmMVzmMt&osid=1&passive=1209600&flowName=WebLiteSignIn&flowEntry=ServiceLogin&dsh=S1209650781%3A1753258709220213 Here, we are going to use ChromeDriver. Find xpath of the button: Method 1: Using Inspect ElementRight 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 librarySet the Web Driver path with the location where you have downloaded the WebDriverExample- "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[text 1="me!"" language="()="Click"][/text]')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 driver.get('https://html.com/tags/button/') # 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: Comment More infoAdvertise with us Y yuvraj_chandra Follow Improve Article Tags : Python Python-selenium Python Selenium-Exercises Practice Tags : python Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 7 min read Python Variables 6 min read Python Operators 6 min read Python Keywords 2 min read Python Data Types 9 min read Conditional Statements in Python 6 min read Loops in Python - For, While and Nested Loops 9 min read Python Functions 9 min read Recursion in Python 6 min read Python Lambda Functions 6 min read Python Data StructuresPython String 6 min read Python Lists 6 min read Python Tuples 6 min read Dictionaries in Python 7 min read Python Sets 10 min read Python Arrays 9 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 6 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 11 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 7 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 10 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like