Click button by text using Python and Selenium
Asked Answered
K

5

25

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

Text = Unlock this result here

Kotick answered 17/2, 2016 at 23:56 Comment(0)
I
34

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.

Imminent answered 18/2, 2016 at 0:12 Comment(5)
Well, they're about 100 different buttons I have to click, and they all have different xpathasKotick
But they have the same text/look the sameKotick
In the example I've given, you're not looking for a given absolute xpath, but for text that buttons contain.Imminent
driver.find_elements_by_xpath("//*[contains(text(), 'Unlock this result here')]").click() Not working, I don't understand?Kotick
Could you provide us with a link to the website you try to interact with? Or show us html code of buttons?Imminent
C
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
    
Comte answered 9/1, 2021 at 17:16 Comment(0)
R
8

I had the following in html:

driver.find_element_by_xpath('//button[contains(text(), "HELLO")]').click()
Risser answered 21/8, 2018 at 21:49 Comment(0)
S
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.

Sulfonmethane answered 7/5, 2019 at 3:11 Comment(0)
B
-1
CheckElementAttribute(driver.FindElement(By.Id("btnCopyBtn")), "enabled", "true", "Copy  Type field");
Brogan answered 2/8, 2023 at 13:30 Comment(1)
You can consider elaborating your answer about how to make use of the code you've provided.Defensible

© 2022 - 2024 — McMap. All rights reserved.