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