Search code examples
pythonseleniumgoogle-chromeselenium-chromedriverpython-webbrowser

How to loop until element clickable


i am using python selenium chrome driver and i am stuck at this.

How can i loop this code until one of the elements is clickable?

Like if its finally clickable it should get clicked and print ("clickable") and if its still not clickable it should print ("Not Clickable")

WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, "//BUTTON[@type='submit'[text()='Zum Warenkorb hinzufügen']"))).click()
WebDriverWait(driver, 150).until(EC.element_to_be_clickable((By.CLASS_NAME, "c-modal__content")))

Solution

  • I am not sure if your use of uppercase button is correct. Use the same syntax as in html.

    One more thing: check your xpath with text(): It should be: //button[@type='submit' and text()='Zum Warenkorb hinzufügen']

    Also, the general case for such loop in the case of one element is:

    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    from selenium.common.exceptions import TimeoutException
    
    wait = WebDriverWait(driver, 15)
    while True:
        try:
            element = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@type='submit' and text()='Zum Warenkorb hinzufügen']")))
            print("clickable")
            element.click()
        except TimeoutException:
            break