Home > Blockchain >  Chrome with Selenium closes immediately after trying to start again after closing it with .quit()
Chrome with Selenium closes immediately after trying to start again after closing it with .quit()

Time:01-21

My program has some sort of memory problem. After a while, chrome stops working properly and displays a page that says "Aw shucks. Ran out of memory." I've been unable to find a solution to this that is in Python, so as a roundabout solution, I have my code setup to close the webdriver process and then restart it to clear the memory and pick up where it left off.

Quitting the webdriver process works fine. However, whenever I try to restart it, it opens for a second, then closes and I don't know why. Here is the code:

from selenium import webdriver
driver = webdriver.Chrome()

#some code operations happen here that have nothing to do with the problem I'm having. It just navigates to different URLs.

if current_iteration >= 1500:
        print('Iteration greater than 1500. Restarting chrome driver...')
        driver.quit()
        current_iteration = 0
        time.sleep(5)
        print('Starting chrome process then waiting 20 seconds...')
        webdriver.Chrome()
        time.sleep(20)

After the program reaches this point, it just says DevTools listening on ws:// followed by a bunch of numbers. The chrome webdriver window then pops up, then closes after one second. The next bit of code is driver.get() with a URL to navigate to, but it exits with a long error, which I can only assume is because the chrome webdriver window is closed and not open.

The webdriver and selenium are both up to date. What could be the problem?

CodePudding user response:

Try to assign the new driver to driver variable in the loop:

from selenium import webdriver
driver = webdriver.Chrome()

#some code operations happen here that have nothing to do with the problem I'm having. It just navigates to different URLs.

if current_iteration >= 1500:
        print('Iteration greater than 1500. Restarting chrome driver...')
        driver.quit()
        current_iteration = 0
        time.sleep(5)
        print('Starting chrome process then waiting 20 seconds...')
        driver = webdriver.Chrome() # I've changed this line
        time.sleep(20)
  •  Tags:  
  • Related