簡體   English   中英

重新啟動驅動程序實例后,Python Selenium Firefox腳本崩潰

[英]Python Selenium Firefox script crashing after restarting driver instance

我有一個循環運行的Python Selenium腳本,它每100次迭代就退出瀏覽器,就像這樣……

def init_driver():
    ffprofile = webdriver.FirefoxProfile("my_profile");
    ffprofile.add_extension(extension="myaddon.xpi")
    return driver

driver = init_driver()

for i, item, in enumerate(item_list):
    check_item(item)
    print ( "" )
    if i == 0:
        print ( "Ignoring First Item" )
    elif i % 100 == 0:
            driver.quit()
            driver = init_driver()

在驅動程序重新啟動期間,它隨機崩潰並出現錯誤...

Traceback (most recent call last):

  File "C:\scripts\main.py", line 118, in <module>
    driver = init_driver()

    File "C:\scripts\main.py", line 98, in init_driver
    driver = webdriver.Firefox(firefox_profile=ffprofile)

    File "C:\Users\john\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 78, in __init__
    self.binary, timeout)

    File "C:\Users\john\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\firefox\extension_connection.py", line 49, in __init__
    self.profile.add_extension()

    File "C:\Users\john\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\firefox\firefox_profile.py", line 91, in add_extension
    self._install_extension(extension)

    File "C:\Users\john\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\firefox\firefox_profile.py", line 287, in _install_extension
    shutil.rmtree(tmpdir)

    File "C:\Users\john\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 488, in rmtree
    return _rmtree_unsafe(path, onerror)

    File "C:\Users\john\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 378, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)

    File "C:\Users\john\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 378, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)

    File "C:\Users\john\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 378, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)

    File "C:\Users\john\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 383, in _rmtree_unsafe
    onerror(os.unlink, fullname, sys.exc_info())

    File "C:\Users\john\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 381, in _rmtree_unsafe
    os.unlink(fullname)

    PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\john\\AppData\\Local\\Temp\\tmpoaizz71l.webdriver.xpi\\platform\\WINNT_x86-msvc\\components\\imehandler.dll'

有時它可以經過數千次迭代而不會崩潰,而其他時候它會在100次后發生

任何人有什么想法嗎?

可能是由於競爭條件,這在臨時配置文件刪除時發生-共享庫仍然訪問該目錄。 Firefox和Selenium是非常異步的,因此有時在過程的各個階段都會發生亂七八糟的情況,您可能會瀏覽“等待元素加載”這類問題。

我猜您有兩個實際的方法可以解決此問題:如果這種情況很少發生,只需在driver_init附近的代碼中添加一個try-except-wait-repeat塊。 這很丑陋,但有時這是努力成果的唯一合理方法。

或者,您可以切換平台:)在Linux之類的系統中,打開的文件通常不阻止其刪除。

另一個解決方法以及@ user3159253的答案 ,在driver.quit()driver = init_driver()之間增加了較短的等待時間。 如果打開新瀏覽器的時間不是很緊迫,則可以等待0.5或1.0秒,然后增大/減小直到發生錯誤。

如果這是過多的實驗,請按照其答案中的建議將其與try...except塊結合使用,並在driver.quit()之前啟動計時器,並檢查發生異常時經過了多少時間。

暫無
暫無

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

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