selenium 问题:OSError: [WinError 6] 句柄无效

问题:

执行多个用例的时候,会抛出异常:web

File "xxxxxx.py", line 16, in get_driver
    driver = webdriver.Chrome(executable_path="chromedriver.exe")
  File "D:\Python3\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 61, in __init__
    self.service.start()
  File "D:\Python3\lib\site-packages\selenium\webdriver\chrome\service.py", line 66, in start
    self.service_args, env=env, stdout=PIPE, stderr=PIPE)
  File "D:\Python3\lib\subprocess.py", line 754, in __init__
    _cleanup()
  File "D:\Python3\lib\subprocess.py", line 474, in _cleanup
    res = inst._internal_poll(_deadstate=sys.maxsize)
  File "D:\Python3\lib\subprocess.py", line 1149, in _internal_poll
    if _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0:
OSError: [WinError 6] 句柄无效。

解决方案:

关闭driver 时 , 使用 driver.quit()代替 driver.close()。chrome

分析:

以前使用 driver.close() 时,用例执行结束有时会不能成功关闭chromedriver.exe,致使后台含有多个chromedriver.exe 进程(能够经过任务管理器查看)。ui

改用 driver.quit()以后,用例执行结束能够看到后台不会含有 chromedriver.exe 进程。该问题就解决了。spa

备注:

区别code

close:只会关闭焦点所在的当前窗口blog

quit:会关闭全部关联的窗口进程

因此推荐使用quit 。get

相关文章
相关标签/搜索