python selenium timeout exception does not pass compiling

Question:

I have some Selenium WebDRiver code that searches for the "Next" button and if it exists, it clicks it. If not, the script should catch TimeoutException and continue.

Code:

from selenium.common.exceptions import TimeoutException

def clicking_next_page():
    btn_next_to_click=WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='next']")))
    try:
        btn_next_to_click.click()
        crawler()
    except TimeoutException:
        pass

Error:

File "C:UsersAdminAppDataLocalProgramsPythonPython310libsite-packagesseleniumwebdriversupportwait.py", line 90, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 
Stacktrace:
Asked By: xlmaster

||

Answers:

You didn’t include the line that actually times out in the try-catch. Simply move the WebDriverWait line down inside the try.

def clicking_next_page():
    try:
        btn_next_to_click = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='next']")))
        btn_next_to_click.click()
        crawler()
    except TimeoutException:
        pass
Answered By: JeffC