0

The below code throwing error.it's suceessfuflly closed but unable to launch the browser again.

driver.close()
driver.get("https://google.com/")
sound wave
  • 3,191
  • 3
  • 11
  • 29

2 Answers2

0

driver.close() closes the browser window which is currently in focus. If there is more than one window opened, then driver.close() will close only the current active window, while the remaining windows will not be closed.

So if you have other windows opened, before loading a new page you first have to switch to one of them using

idx = ... # index of one of your windows
driver.switch_to.window( driver.window_handles[idx] )

If there aren't other windows opened, then you have to quit the driver and start a new one.

driver.quit()
driver = webdriver.Chrome(...)
sound wave
  • 3,191
  • 3
  • 11
  • 29
  • driver.close() browser = webdriver.Chrome() browser.get("https://google.com/") The above code resolve the issue – Murali Raju Nadakuditi Jan 18 '23 at 14:21
  • @MuraliRajuNadakuditi If you don't want to use `driver` anymore it is better to run `driver.quit()` which closes it completely – sound wave Jan 18 '23 at 16:41
  • I'd second that... using quit() should be more reliable especially across different drivers. I'd also recommend using a sleep in between quit() and launching a new driver if you see any seemingly random exceptions being thrown on startup. (This gives the browser time to cleanup files/folders used during the 1st session.) – pcalkins Jan 18 '23 at 18:01
0

driver.close()

driver.close() closes the current top-level browsing context. If there are no more open top-level browsing contexts, then it closes the session.

As driver.close() closes only the current top-level browsing context, if there are more top-level browsing context then you need to switch tab to any other top-level browsing context.

Else if there are no more top-level browsing context then driver.close() closes the session and you have to reinitialize the WebDriver and Browser client. This is equivalent to invoking driver.quit()

  • Java snippet:

    driver.close()
    WebDriver driver = new ChromeDriver();
    
  • Python snippet:

    driver.close()
    driver = webdriver.Chrome()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352