1

My selenium python script can't click the button with either click() or `driver.execute_script("arguments[0].click();", continue_). It worked for previous buttons but this particular button is only detected but can't be clicked.

Here is the code:

WebDriverWait(driver, 60).until(EC.presence_of_element_located(
    (By.XPATH, "//button[contains(text(),'Continue')]")))
continue_ = driver.find_element(
    By.XPATH, "//button[contains(text(),'Continue')]")
driver.execute_script("arguments[0].click();", continue_)

enter image description here

Update: The answers didn't work for me. I tried adding prints to see where it stops.

        WebDriverWait(driver, 60).until(EC.presence_of_element_located(
            (By.XPATH, "//button[contains(text(),'Continue')]")))
        print("Presence Located")
        continue_ = driver.find_element(
            By.XPATH, "//button[contains(text(),'Continue')]")
        print("Continue Button Found")
        driver.execute_script("arguments[0].click();", continue_)
        print("Continue Button Clicked")

My console displays all prints till the "Continue Button Clicked" but is still not clicking the Continue so my bot can't progress through it's script. I don't know if it's any help but can't think of anything else.

Update Tried using is_displayed() on the continue button it returned True.

1 Answers1

1

The <button> element have the innerText spanned over multiple lines.


Solution

To click on the element instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using XPATH and starts-with():

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@type='submit' and starts-with(., 'Continue')]"))).click()
    
  • Using XPATH and contains():

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@type='submit' and contains(., 'Continue')]"))).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