Click button by text using Python and Selenium

耗尽温柔 提交于 2019-12-10 02:15:46

问题


Is it possible to click multiply buttons with the same text with Selenium?


回答1:


You can find all buttons by text and then execute click() method for each button in a for loop.

Using this SO answer it would be something like this:

buttons = driver.find_elements_by_xpath("//*[contains(text(), 'My Button')]")

for btn in buttons:
    btn.click()

I also recommend you take a look at Splinter which is a nice wrapper for Selenium.

Splinter is an abstraction layer on top of existing browser automation tools such as Selenium, PhantomJS and zope.testbrowser. It has a high-level API that makes it easy to write automated tests of web applications.




回答2:


I had the following in html:

driver.find_element_by_xpath('//button[contains(text(), "HELLO")]').click()



回答3:


@nobodyskiddy , Try to use driver.find_element ( if you have single button option ) , use index to click() when you are using driver.find_elements , find_elements will return array to webelement values , so you have to use index to select or click.



来源:https://stackoverflow.com/questions/35470171/click-button-by-text-using-python-and-selenium

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