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.

3
  • Is the content in a frame? If so you might need to switch_to.frame(). Commented May 1, 2022 at 16:23
  • Please update the question with HTML source Commented May 1, 2022 at 16:23
  • Updated the question to another button but with the same problem. Commented May 1, 2022 at 20:38

1 Answer 1

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
    
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but it didn't work for me.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.