簡體   English   中英

帶有 Selenium 的 Chrome 在用.quit() 關閉后嘗試再次啟動后立即關閉

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

我的程序有某種 memory 問題。 過了一會兒,chrome 停止正常工作並顯示一個頁面,上面寫着“Aw shucks. Ran out of memory”。 我一直無法在 Python 中找到解決方案,因此作為一個迂回的解決方案,我設置了代碼以關閉 webdriver 進程,然后重新啟動它以清除 memory 並從中斷處繼續。

退出 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM