Python Selenium – Find Button by text Read Courses Practice Improve Improve Improve Like Article Like Save Article Save Report issue 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://sites.google.com/a/chromium.org/chromedriver/downloads 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’)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: https://media.geeksforgeeks.org/wp-content/uploads/20210227233751/Find-Button-By-text.mp4 Last Updated : 03 Mar, 2021 Like Article Save Article Previous Selenium - Search for text on page Next Web Scraping Tables with Selenium and Python Share your thoughts in the comments Add Your Comment Please Login to comment...