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

Try this XPath:

"//button[@class='three-state-item btn btn-default'][.='Outliers']".


There are two ways :

  1. By using text() method:

browser.find_element(By.XPATH,'//button[text()="Outliers"]')

  1. By using normalize-space() method:

browser.find_element(By.XPATH, '//button[normalize-space()="Outliers"]')

Note : It is always better to use normalize-space() method as it will work even if there are spaces present at the start of your text or at the end of text, because normalize-space() method trim the left and right side spaces

For More information on Normalize-space()


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:

  • https://stackoverflow.com/a/70586710/2026508