Is it possible to click multiply buttons with the same text with Selenium?
5 Answers
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.
-
Well, they're about 100 different buttons I have to click, and they all have different xpathas – Feb 18 '16 at 00:16
-
But they have the same text/look the same – Feb 18 '16 at 00:16
-
2In the example I've given, you're not looking for a given *absolute* xpath, but for text that buttons contain. – kchomski Feb 18 '16 at 00:30
-
driver.find_elements_by_xpath("//*[contains(text(), 'Unlock this result here')]").click() Not working, I don't understand? – Feb 18 '16 at 01:16
-
Could you provide us with a link to the website you try to interact with? Or show us html code of buttons? – kchomski Feb 18 '16 at 13:19
To locate and click a <button> element by it's text you can use either of the following Locator Strategies:
Using xpath and
text():driver.find_element_by_xpath("//button[text()='button_text']").click()Using xpath and
contains():driver.find_element_by_xpath("//button[contains(., 'button_text')]").click()
Ideally, to locate and click a <button> element by it's text you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using XPATH and
text():WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='button_text']"))).click()Using XPATH and
contains():WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'button_text')]"))).click()Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
Update
To locate all the <button> elements by the text you can use either of the following Locator Strategies:
Using xpath and
text():for button in driver.find_elements_by_xpath("//button[text()='button_text']"): button.click()Using xpath and
contains():for button in driver.find_elements_by_xpath("//button[contains(., 'button_text')]"): button.click()
Ideally, to locate all the <button> elements by the text you need to induce WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following Locator Strategies:
Using XPATH and
text():for button in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//button[text()='button_text']"))): button.click()Using XPATH and
contains():for button in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//button[contains(., 'button_text')]"))): button.click()Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
- 183,867
- 41
- 278
- 352
I had the following in html:
driver.find_element_by_xpath('//button[contains(text(), "HELLO")]').click()
- 271
- 4
- 13
@nobodyskiddy, Try to use driver.find_element ( if you have a single button option ), use index to click() when you are using driver.find_elements, find_elements will return array to web element values, so you have to use the index to select or click.
- 1,716
- 10
- 23
- 11
- 6
CheckElementAttribute(driver.FindElement(By.Id("btnCopyBtn")), "enabled", "true", "Copy Type field");
- 2,452
- 6
- 27
- 36
-
You can consider elaborating your answer about how to make use of the code you've provided. – Dev Bhuyan Aug 06 '23 at 09:44
