PermissionError: [WinError 32] 另一個程序正在使用此文件,進程無法訪問。: 'C:\\Users\\video\\AppData\\Local\\Temp\\tmpfipzk8ma'--問題解決


學習python+selenium總是遇到各種問題

代碼如下:

from selenium import webdriver
browser = webdriver.PhantomJS() #瀏覽器初始化;Win下需要設置phantomjs路徑,linux下置空即可
url = 'http://www.zhidaow.com' # 設置訪問路徑
browser.get(url) # 打開網頁
title = browser.find_elements_by_xpath('//h2') # 用xpath獲取元素

for t in title: # 遍歷輸出
print(t.text) # 輸出其中文本
print(t.get_attribute('class')) # 輸出屬性值

browser.quit()
運行后報錯:

  File "C:\Users\video\AppData\Roaming\Python\Python34\site-packages\selenium\webdriver\common\service.py", line 151, in __del__
  File "C:\Users\video\AppData\Roaming\Python\Python34\site-packages\selenium\webdriver\common\service.py", line 127, in stop
  File "C:\Users\video\AppData\Roaming\Python\Python34\site-packages\selenium\webdriver\phantomjs\service.py", line 68, in send_remote_shutdown_command
PermissionError: [WinError 32] 另一個程序正在使用此文件,進程無法訪問。: 'C:\\Users\\video\\AppData\\Local\\Temp\\tmpfipzk8ma'

 

解決辦法:(我是用pycharm)

編輯C:\Users\video\AppData\Roaming\Python\Python34\site-packages\selenium\webdriver\phantomjs\service.py的文件

找到:

def send_remote_shutdown_command(self):
if self._cookie_temp_file:
os.remove(self._cookie_temp_file)
修改如下:
def send_remote_shutdown_command(self):

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM