24

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

Text = Unlock this result here

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

5 Answers5

31

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.

Community
  • 1
  • 1
kchomski
  • 2,872
  • 20
  • 31
  • 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
  • 2
    In 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
9

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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
8

I had the following in html:

driver.find_element_by_xpath('//button[contains(text(), "HELLO")]').click()
Nisheeth
  • 271
  • 4
  • 13
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.

Darkknight
  • 1,716
  • 10
  • 23
-1
CheckElementAttribute(driver.FindElement(By.Id("btnCopyBtn")), "enabled", "true", "Copy  Type field");
LW001
  • 2,452
  • 6
  • 27
  • 36