Andersson Andersson - 7 months ago 15
Python Question

Permission Error if to use PhantomJS

I have a

Selenium-Python
script that works well with
Firefox
and
Chrome
, but sometimes raise following exception if to use
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'


The point of script is to loop through list of files: start browser session for each file, send file for analysis, get and save results, close browser. Issue appears while session re-opening (between
driver.close()
and
driver = webdriverPhantomJS()
) for next iteration in about 50% cases...

Any assumption about what could cause this issue and how it can be fixed?

P.S. Let me know if any additional info required

Answer

As I found out problem is that system tries to remove temporary file created during browser session, but not always successfully. Solved by changing function in selenium.webdriver.phantomjs.service.py like follow:

was

def send_remote_shutdown_command(self):
    if self._cookie_temp_file:
            os.remove(self._cookie_temp_file)

after adding try/except construction:

def send_remote_shutdown_command(self):
    try:
        if self._cookie_temp_file:
            os.remove(self._cookie_temp_file)
    except PermissionError:
        pass

This will allow to avoid temporary file remove if webdriver not able to do so. File is actually empty (0kb), so even big number of such temp files will not harm system