【问题标题】:Proper driver.quit() to enable reopening the driver正确的 driver.quit() 以启用重新打开驱动程序
【发布时间】:2021-11-14 03:03:32
【问题描述】:

在我的机器人完成它的工作后,我启动 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 秒后,我想再次打开 webdriver,但触发了此错误。优雅地关闭 webdriver 并重新启动它的正确方法是什么?

【问题讨论】:

  • 只需将其更改为 driver.close() 看看是否有帮助。
  • 我之所以没有使用 driver.close() 是因为我有 2 个标签,而 driver.close() 只是关闭了打开的标签。
  • @Emoyaki 您需要提供更多信息。第二个选项卡是否也由 webdriver 控制?你是从同一个程序控制它吗?
  • @Dillanm 是的,基本上我的工具发送消息,第一个选项卡是一个表单,机器人从中获取我想要发送消息的人的姓名,第二个选项卡是它发送消息的位置跨度>

标签: python selenium bots


【解决方案1】:

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

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/")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多