2

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.

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. What seems to be wrong?

Cassano
  • 253
  • 5
  • 36

1 Answers1

2

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. If you want to catch exceptions in child thread it is probably worth reading Catch a thread's exception in the caller thread?

Alexey R.
  • 8,057
  • 2
  • 11
  • 27