python - 尝试调用 Selenium 时超时异常失败

标签 python selenium

我故意尝试找到错误的元素,以便可以调用 TimeoutException,然后再次调用该函数,但我看到了错误堆栈跟踪,但它不起作用。

代码:

def open_browser_func():
    global driver
    driver = webdriver.Chrome(service=ser)
    
    wait = WebDriverWait(driver, 20)

    driver.get("https://twitter.com/i/flow/login")
    print("Opening login page")

    try:
        def sign_in_acc():
            loginuser  = wait.until(EC.visibility_of_element_located((By.NAME, "textt")))
            loginuser.send_keys("Username", Keys.RETURN)

        sign_in_acc_timer = threading.Timer(5, sign_in_acc)
        sign_in_acc_timer.start()

    except TimeoutException:
        print("Username input crashed. Retrying now...")

        def retry_sign_in():
            driver.quit()
            return open_browser_func()
        retrytimer = threading.Timer(5, retry_sign_in)
        retrytimer.start()

    return driver

错误:

  File "C:\Users\Cassano\AppData\Roaming\Python\Python38\site-packages\selenium\webdriver\support\wait.py", line 89, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Stacktrace:
Backtrace:
        Ordinal0 [0x00F46903+2517251]
        Ordinal0 [0x00EDF8E1+2095329]
        Ordinal0 [0x00DE2848+1058888]
        Ordinal0 [0x00E0D448+1233992]
        Ordinal0 [0x00E0D63B+1234491]
        Ordinal0 [0x00E37812+1406994]
        Ordinal0 [0x00E2650A+1336586]
        Ordinal0 [0x00E35BBF+1399743]
        Ordinal0 [0x00E2639B+1336219]
        Ordinal0 [0x00E027A7+1189799]
        Ordinal0 [0x00E03609+1193481]
        GetHandleVerifier [0x010D5904+1577972]
        GetHandleVerifier [0x01180B97+2279047]
        GetHandleVerifier [0x00FD6D09+534521]
        GetHandleVerifier [0x00FD5DB9+530601]
        Ordinal0 [0x00EE4FF9+2117625]
        Ordinal0 [0x00EE98A8+2136232]
        Ordinal0 [0x00EE99E2+2136546]
        Ordinal0 [0x00EF3541+2176321]
        BaseThreadInitThunk [0x770EFA29+25]
        RtlGetAppContainerNamedObjectPath [0x77B47A9E+286]
        RtlGetAppContainerNamedObjectPath [0x77B47A6E+238]

我看到的只是日志中的 TimeoutException,但不是我打算调用来处理它的内容。好像出了什么问题?

最佳答案

您正在等待不同线程中的元素。一旦您调用 sign_in_acc_timer.start(),您就会启动并行线程,其中正在执行等待,并且 open_browser_func() 完成。如果您想捕获子线程中的异常,可能值得阅读 Catch a thread's exception in the caller thread?

关于python - 尝试调用 Selenium 时超时异常失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70466333/

相关文章:

javascript - 如何将原始 javascript 对象转换为字典?

python - 使用 Selenium : How to keep logged in after closing Driver in Python in whatsapp

python - 无法处理一些关键的链接

python - 不平衡数据和加权交叉熵

python - 在 PyCharm IDE 中添加 Spark 包

python - 用 Python 在 Google App Engine 中实现 Comet/Server 推送

python - 从最后一列向动态树添加大小

java - 使用 Selenium 独立服务器 3.9.1 在 Eclipse Photon 中导入时出现 "The import org cannot be resolved"错误

javascript - 类型错误 : Cannot read property 'constructor' of undefined

java - 使用 Selenium 截取特定元素