python selenium超时异常没有通过编译

问题描述 投票:0回答:1

我有一些搜索“下一步”按钮的 Selenium WebDRIver 代码,如果存在,则单击它。如果没有,脚本应该捕获

TimeoutException
并继续。

代码:

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

错误:

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:
python selenium-webdriver timeoutexception
1个回答
1
投票

您没有在

try-catch
中包含实际超时的行。只需将
WebDriverWait
线向下移动到
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
© www.soinside.com 2019 - 2024. All rights reserved.