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

南楼画角 提交于 2020-06-24 22:50:31

问题


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?


回答1:


Another example XPath:

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



回答2:


Try this XPath:

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




回答3:


There are two ways :

  1. By using text() method -
    browser.find_element_by_xpath('//button[text()="Outliers"]')
  2. 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



来源:https://stackoverflow.com/questions/49906237/how-to-find-button-with-selenium-by-its-text-inside-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!