Home > Net >  How to close the browser and launch the browser again using selenium with python? Can you give sampl
How to close the browser and launch the browser again using selenium with python? Can you give sampl

Time:01-19

The below code throwing error.it's suceessfuflly closed but unable to launch the browser again. driver.close() driver.get("https://google.com/")

The below code throwing error.it's suceessfuflly closed but unable to launch the browser again. driver.close() driver.get("https://google.com/")

CodePudding user response:

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(...)

CodePudding user response:

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()
    
  • Related