3

for one of my scripts, I need to restart a browser (chromeDriver) session at some random point.

I have tried to restart the webdriver in a test script without success.

def start_browser():
    driver.get('https://www.google.com/')
    time.sleep(1)

def close():   
    driver.quit()
    driver.get("http://www.google.com/")


for x in range (1,5):
    start_browser()
    time.sleep(5)
    close()
    time.sleep(5)

For some reason, the script does not start a new instance of the browser, instead it crashes. I think I am missing out some vital selenium command, but I could not find anything on the official page.

HansDampf
  • 121
  • 3
  • 12
  • 1
    the browser starts when you instantiate the driver. You are quitting the driver, which shuts down both the driver and the browser before trying to call .get()... – pcalkins Jul 18 '19 at 20:51

3 Answers3

1

The question is a bit old, but for anyone still asking As mentioned in a previous comment, you will need to reinitiate the driver. Sometimes this is the easier way to clear all the cookies and run a test.

Assuming the funcs that you gave are from a class with a driver attribute

def start_browser(self):
    self.driver.get('https://www.google.com/')
    time.sleep(1)

def close(self):   
    self.driver.quit()
    self.driver = webdriver.Chrome(*driver_params)
    self.driver.get("http://www.google.com/")


for x in range (1,5):
    start_browser()
    time.sleep(5)
    close()
    time.sleep(5)

This isn't how 1 would actually write it IMO, but the restarting of the browser works, it's also best to an incognito mode to the driver if you want all cookies to be cleared too.

0

You dont need the line driver.quit()simply write driver.get("http://www.google.com/").

def close():   
    driver.get("http://www.google.com/")

If you would write it like you did, it would close the driver and you would have to initialise the driver again.

Dakkaron
  • 5,930
  • 2
  • 36
  • 51
  • Thanks, but this will just open a new url in the browser, and I need to start a new instance, using Driver as the argument – HansDampf Jul 22 '19 at 10:07
-1
for r in range(1, 2):
    driver.get("https://stackoverflow.com/")
    time.sleep(5)
    driver.close()
    driver.get("https://stackoverflow.com/")