2016-04-09 9 views
2

ドライバインスタンスを再起動した後、私はループ内で実行されているPythonのセレンスクリプトは、それがブラウザにこのようなすべての100回の反復を終了しますが...PythonのセレンFirefoxのスクリプトクラッシュ

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

誰も後にすることができます...エラーでクラッシュ何が起きているのでしょうか?

答えて

2

これは、競合状態のため一時的なプロファイルの削除で発生する可能性があります。共有libtaryは引き続きディレクトリにアクセスします。 FirefoxとSeleniumは非常に非同期なので、プロセスのさまざまな段階でちょうど起こることがあります。「要素がロードされるのを待っている」などのような質問を見ることができます。

問題を処理する2つの実用的な選択肢があると思います。これが起こる/ほとんどない場合は、driver_init付近のコードにtry-except-wait-repeatブロックを追加するだけです。これは醜いですが、努力の結果として唯一の合理的な方法です。

また、plaformを切り替えることもできます:) Linuxのようなシステムでは、開いたファイルは通常、削除をブロックしません。

+0

_ "たまにはたまにしかない" _ _実際にはそうです。 – aneroid

2

@user3159253's answerと共にもう1つの回避策は、driver.quit()driver = init_driver()の間に短い待ち時間を追加します。新しいブラウザを開くことが時間的に重要でない場合は、0.5秒または1.0秒待ってからエラーが発生するまで増減してください。

これはあまりにも多くの実験では、try...exceptブロックと組み合わせて答えを提示し、driver.quit()の前にタイマーを開始し、例外が発生した時間を確認します。

関連する問題