简体   繁体   English

尝试调用它时超时异常失败 Selenium

[英]Timeout Exception fails when trying to call it Selenium

I'm trying intentionally to locate wrong element so that I can call TimeoutException, and then call the function again, but instead I see the error stacktrace and it doesn't work.我试图故意定位错误的元素,以便我可以调用 TimeoutException,然后再次调用 function,但我看到了错误堆栈跟踪,但它不起作用。

Code:代码:

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

Error:错误:

  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]

All I see is the TimeoutException in log, but not what I intend to call to handle it.我所看到的只是日志中的 TimeoutException,但不是我打算调用来处理它的。 What seems to be wrong?似乎有什么问题?

You are waiting for element in different thread.您正在等待不同线程中的元素。 As soon as you call sign_in_acc_timer.start() you start parallel thread where waiting is being executed and your open_browser_func() finishes.一旦你调用sign_in_acc_timer.start()你就会启动并行线程,等待正在执行并且你的open_browser_func()完成。 If you want to catch exceptions in child thread it is probably worth reading Catch a thread's exception in the caller thread?如果您想在子线程中捕获异常,可能值得一读在调用者线程中捕获线程的异常?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM