适当的driver.quit()以启用重新打开驱动程序



在我的bot完成它的工作后,我启动driver.quit()以终止会话,因此我可以在一段时间后再次打开驱动程序来启动一个新的会话,但我得到以下错误:

MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=56717): Max retries exceeded with url: /session/042df57c7e963e8f582735ae8242a5df/window (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001BDB7117F40>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))

我代码:

if loopNumber == 15:
timeStop = timeit.default_timer()
execution_time = timeStop - timeStart
print("Program Executed in "+str(execution_time/60)+" Minutes") # It returns time in seconds
driver.stop_client()
driver.quit()
time.sleep(600)

600秒后,我想再次打开web驱动程序,但触发此错误。什么是正确的方式来优雅地关闭webdriver并重新启动它?

如果你想结束进程并重新开始一个新的会话,你需要重新初始化驱动程序对象。

driver = webdriver.Chrome(executable_path="path to chromedriver.exe")
driver.get("https://www.youtube.com/")
driver.close() # or driver.quit()
driver = webdriver.Chrome(executable_path="path to chromedriver.exe")
driver.get("https://www.youtube.com/")

但是你也可以使用相同的会话打开一个新的URL。获得子窗口并关闭相同的。关注父窗口,再次执行driver.get(URL)

注意:浏览器不关闭。

driver = webdriver.Chrome(executable_path="path to chromedriver.exe")
driver.get("https://google.com")
#Code to close the child window and focus on the parent window.
driver.get("https://www.youtube.com/")

最新更新