26

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

Text = Unlock this result here

5 Answers 5

37

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Well, they're about 100 different buttons I have to click, and they all have different xpathas
But they have the same text/look the same
In the example I've given, you're not looking for a given absolute xpath, but for text that buttons contain.
driver.find_elements_by_xpath("//*[contains(text(), 'Unlock this result here')]").click() Not working, I don't understand?
Could you provide us with a link to the website you try to interact with? Or show us html code of buttons?
11

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

8

I had the following in html:

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

Comments

1

@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.

Comments

-1
CheckElementAttribute(driver.FindElement(By.Id("btnCopyBtn")), "enabled", "true", "Copy  Type field");

1 Comment

You can consider elaborating your answer about how to make use of the code you've provided.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.