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:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\support\wait.py", line 90, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Stacktrace:
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