How to find button with Selenium by its text inside (Python)?

Issue

I have the following three buttons that I can’t figure out how to grab the text that is inside of them (e.g Outliers). I tried browser.find_element_by_link_text("Outliers").click(), but got “Unable to locate element” error. How can I do it?

enter image description here

Solution

See: find_element_by_* commands are deprecated in selenium

In newer versions of selenium try:

from selenium.webdriver.common.by import By
browser.find_element(By.XPATH, '//button[text()="Outliers"]')

older versions of selenium:

browser.find_element_by_xpath('//button[text()="Outliers"]')

To update ALL of the older versions I found a nifty regex here, and then just fixup the import:

Answered By – jmunsch

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published