Python Selenium Webdriver - Try except loop
The answer on your specific question is:
from selenium.common.exceptions import NoSuchElementExceptionlink = Nonewhile not link: try: link = driver.find_element_by_xpath(linkAddress) except NoSuchElementException: time.sleep(2)
However, there is a better way to wait until element appears on a page: waits
Another way could be.
from selenium.common.exceptions import TimeoutExceptionfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.common.by import Bytry: element = WebDriverWait(driver, 2).until( EC.presence_of_element_located((By.XPATH, linkAddress)) )except TimeoutException as ex: print ex.message
Inside the WebDriverWait call, put the driver variable and seconds to wait.