gpt4 book ai didi

Python Selenium Firefox 脚本在重新启动驱动程序实例后崩溃

转载 作者:行者123 更新时间:2023-11-28 22:37:30 27 4
gpt4 key购买 nike

我有一个循环运行的 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 block 。这很丑陋,但有时这是努力结果的唯一合理方式。

或者,您可以切换平台 :) 在像 Linux 这样的系统中,打开的文件通常不会阻止它们的删除。

关于Python Selenium Firefox 脚本在重新启动驱动程序实例后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36522992/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com