Chrome 使用 Selenium 在使用 .quit() 关闭后尝试重新启动后立即关闭

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

我的程序有某种内存问题。一段时间后,chrome 停止正常工作并显示一个页面,上面写着“糟糕。运行 内存不足。”我一直无法在 Python 中找到解决方案,因此作为迂回解决方案,我将代码设置为关闭 webdriver 进程,然后重新启动它以清除内存并从它离开的地方开始关闭。

退出 webdriver 进程正常。但是,每当我尝试重新启动它时,它都会打开一秒钟,然后关闭,我不知道为什么。这是代码:

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)

程序执行到这一步后,它只是说 DevTools listening on ws:// 后面跟着一串数字。 chrome webdriver window 然后弹出,然后在一秒钟后关闭。下一段代码是 driver.get(),带有要导航到的 URL,但它退出时出现一个很长的错误,我只能假设这是因为 chrome webdriver window 是关了没开。

webdriver 和 selenium 都是最新的。可能是什么问题?

尝试将新驱动程序分配给循环中的 driver 变量:

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)