How do I loop finding an element so that it keeps on checking for one in Python Selenium?

Viewed 32

basically I am trying to loop driver.find_element_by_id("add-to-cart-button").click() so that it does this every 5 seconds even if it cannot find the element. All I have been able to put together is (there is more code but this is the base problem):

from selenium.common.exceptions import ElementNotVisibleException, WebDriverException, NoSuchElementException
import time
from selenium import webdriver

driver=webdriver.Chrome(executable_path="C:\webdrivers\chromedriver.exe")
driver.get("http://www.amazon.co.uk")

try:
    driver.find_element_by_id("add-to-cart-button").click()
except (ElementNotVisibleException, WebDriverException, NoSuchElementException):
    time.sleep(5)
    driver.find_element_by_id("add-to-cart-button").click()
0 Answers
Related