And*_*son 2 python selenium phantomjs selenium-webdriver
我有一个Selenium-Python
适用于Firefox
和的脚本Chrome
,但有时会引发以下异常,如果使用PhantomJS
:
Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.phant
omjs.service.Service object at 0x000000000439FE80>>
Traceback (most recent call last):
File "C:\Python34\Lib\site-packages\selenium\webdriver\common\service.py", line 151, in __del__
self.stop()
File "C:\Python34\Lib\site-packages\selenium\webdriver\common\service.py", line 127, in stop
self.send_remote_shutdown_command()
File "C:\Python34\Lib\site-packages\selenium\webdriver\phantomjs\service.py", line 68, in send_remote_shutdown_command
os.remove(self._cookie_temp_file)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\OB865~1.LUC\\AppData\\Local\\Temp\\tmpe6zrcjem'
Run Code Online (Sandbox Code Playgroud)
脚本的要点是遍历文件列表:为每个文件启动浏览器会话,发送文件进行分析,获取并保存结果,关闭浏览器.在大约50%的情况下会话重新打开(在driver.close()
和之间driver = webdriverPhantomJS()
)下一次迭代时出现问题...
关于什么可能导致这个问题及其如何解决的任何假设?
PS如果需要任何其他信息,请告诉我
我发现问题是系统试图删除在浏览器会话期间创建的临时文件,但并不总是成功.通过改变功能解决selenium.webdriver.phantomjs.service.py
如下:
是
def send_remote_shutdown_command(self):
if self._cookie_temp_file:
os.remove(self._cookie_temp_file)
Run Code Online (Sandbox Code Playgroud)
添加try/except
施工后:
def send_remote_shutdown_command(self):
try:
if self._cookie_temp_file:
os.remove(self._cookie_temp_file)
except PermissionError:
pass
Run Code Online (Sandbox Code Playgroud)
如果webdriver
不能这样做,这将允许避免临时文件删除.文件实际上是空的(0kb),因此即使大量此类临时文件也不会损害系统
归档时间: |
|
查看次数: |
1531 次 |
最近记录: |