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.
5 Comments
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
Comments
CheckElementAttribute(driver.FindElement(By.Id("btnCopyBtn")), "enabled", "true", "Copy Type field");
