Emoyaki
Emoyaki

Reputation: 105

Proper driver.quit() to enable reopening the driver

After my bot is done doing it's job I initiate driver.quit() to terminate the session, so I can start a new one by opening the driver again after some time, but I get the following error:

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'))

My code:

    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)

after 600 seconds I want to open the webdriver again but this error is triggered. What will be the proper way to gracefully close webdriver and start it again?

Upvotes: 1

Views: 1463

Answers (1)

pmadhu
pmadhu

Reputation: 3433

If you want to end the process and start a new session again, you need to reinitialize driver object.

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

But you can also use same session to open a new URL. Get the Child window and close the same. Focus on the parent window and do driver.get(URL) again.

Note: The browser does not close.

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

Upvotes: 2

Related Questions