Python+Selenium遭遇NoSuchWindowException异常求助
问题:Selenium Chrome驱动触发NoSuchWindowException错误
以下是用于登录测试环境的Python代码:
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...
由于需要在浏览器中处理3D模型,长期运行易出现Chrome内存溢出和服务器超时,因此添加了强制重启浏览器的逻辑:
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()
这段代码原本稳定运行数周,但近期突然在signIn函数的email = driver.find_element行抛出如下错误:
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)
该错误可能出现在首次运行或后续执行过程中,已排查以下内容:
- 桌面会打开新的浏览器实例,驱动实例化正常
- 仅存在一个窗口句柄,无会话指向错误的情况
- 系统中仅运行一个ChromeDriver.exe实例
- 仅在
signIn方法中对全局driver变量赋值 - ChromeDriver版本与Chrome版本(139.x)匹配且为最新版
更新1
已尝试重启和更新VS Code,问题未解决。测试需处理100个3D模型,此前会在处理第一个模型或第五个模型(首次触发驱动重启逻辑时)失败。
重启电脑后问题暂时消失,但在第6次触发驱动重启(处理完30个模型)后再次出现。因此判断重启逻辑本身无问题,已成功执行5次。
附上完整错误栈信息:
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) Stacktrace: GetHandleVerifier [0x0x7ff73c1b6b25+79621] GetHandleVerifier [0x0x7ff73c1b6b80+79712] (No symbol) [0x0x7ff73bf4c0ea] (No symbol) [0x0x7ff73bf24601] (No symbol) [0x0x7ff73bfd25fe] (No symbol) [0x0x7ff73bff2d02] (No symbol) [0x0x7ff73bfcaf13] (No symbol) [0x0x7ff73bf94151] (No symbol) [0x0x7ff73bf94ee3] GetHandleVerifier [0x0x7ff73c47683d+2962461] GetHandleVerifier [0x0x7ff73c470b5d+2938685] GetHandleVerifier [0x0x7ff73c48f71d+3064573] GetHandleVerifier [0x0x7ff73c1d0c6e+186446] GetHandleVerifier [0x0x7ff73c1d8a3f+218655] GetHandleVerifier [0x0x7ff73c1bf914+115956] GetHandleVerifier [0x0x7ff73c1bfac9+116393] GetHandleVerifier [0x0x7ff73c1a5ef8+10968] BaseThreadInitThunk [0x0x7ff95197e8d7+23] RtlUserThreadStart [0x0x7ff95327c34c+44] File "C:\..\..\GenerateTour.py", line 37, in signIn email = driver.find_element(By.ID, "email") File "C:\..\..\GenerateTour.py", line 215, in generateAutoTours await signIn() File "C:\..\..\GenerateTour.py", line 448, in main await generateAutoTours(ProcessingType.FOLDER, "97zbnRzBysu") File "C:\..\..\GenerateTour.py", line 454, in <module> asyncio.run(main()) ~~~~~~~~~~~^^^^^^^^ selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed from unknown error: web view not found (Session info: chrome=139.0.7258.128) Stacktrace: GetHandleVerifier [0x0x7ff73c1b6b25+79621] GetHandleVerifier [0x0x7ff73c1b6b80+79712] (No symbol) [0x0x7ff73bf4c0ea] (No symbol) [0x0x7ff73bf24601] (No symbol) [0x0x7ff73bfd25fe] (No symbol) [0x0x7ff73bff2d02] (No symbol) [0x0x7ff73bfcaf13] (No symbol) [0x0x7ff73bf94151] (No symbol) [0x0x7ff73bf94ee3] GetHandleVerifier [0x0x7ff73c47683d+2962461] GetHandleVerifier [0x0x7ff73c470b5d+2938685] GetHandleVerifier [0x0x7ff73c48f71d+3064573] GetHandleVerifier [0x0x7ff73c1d0c6e+186446] GetHandleVerifier [0x0x7ff73c1d8a3f+218655] GetHandleVerifier [0x0x7ff73c1bf914+115956] GetHandleVerifier [0x0x7ff73c1bfac9+116393] GetHandleVerifier [0x0x7ff73c1a5ef8+10968] BaseThreadInitThunk [0x0x7ff95197e8d7+23] RtlUserThreadStart [0x0x7ff95327c34c+44]
更新2
怀疑Python的Selenium驱动或Chrome存在新bug。调试driver.get(loginURL)代码时发现,向客户端发送URL(URLLib3的connection.py第508行self.send(chunk))后,响应对象返回200(URLLib3的connectionpool.py第534行response = conn.getresponse()),但浏览器UI的地址栏从未显示该URL。
恳请各位提供相关见解。
内容的提问来源于stack exchange,提问作者Kevin McDowell
相关产品推荐
相关产品推荐

