[Python] NoSuchWindowException with Python and Selenium

Discussão em 'Python' iniciado por Stack, Agosto 14, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following Python code:

    async def signIn():
    """Signs in to test environment based on the credentials provided with the developer tools open and logging started
    """
    global driver
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--enable-logging')
    chrome_options.add_argument("--auto-open-devtools-for-tabs")
    chrome_options.set_capability('goog:loggingPrefs', {'performance': 'ALL'})
    driver = webdriver.Chrome(options=chrome_options)
    driver.execute_cdp_cmd("Runtime.enable", {})
    driver.execute_cdp_cmd("Log.enable", {})
    driver.get(loginURL)
    await waitForElementVisibilityBy(driver, By.ID, "email", 10)
    email = driver.find_element(By.ID, "email")
    # additional code to actually sign-in...


    Later in the same project, because I'm working with 3D models in the browser, I forcibly close the browser to avoid Chrome out of memory errors (which it frequently occur if I let it run too long) and server timeouts:

    if counter > 1 and counter % 5 == 0:
    print("Sleeping for 10 seconds to prevent out of memory errors and server timeouts")
    driver.quit() # resetting Chrome
    os.system("taskkill /f /im chromedriver.exe")
    await saveResults(resultJson, "results.json") # make sure we don't lose any results if there's an error
    time.sleep(10)
    await signIn()


    This code has been running fine for weeks. Suddenly, it is failing in the signIn function on the line that begins email = driver.find_element with this error:

    Exception has occurred: NoSuchWindowException
    Message: no such window: target window already closed
    from unknown error: web view not found
    (Session info: chrome=139.0.7258.128)


    It frequently fails on the first pass or on subsequent passes, after it's been running. Things I've already checked:

    • There is a new open browser instance on the desktop, so it's been instantiated correctly.
    • There is only one window handle available, so it can't be looking for a different browser session.
    • There's only one instance of ChromeDriver.exe running.
    • The signIn method code is the only place in the code where drive is being set.

    Any ideas what could be causing this?

    Continue reading...

Compartilhe esta Página