学习python+selenium老是遇到各类问题python
代码以下:linux
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'web
解决办法:(我是用pycharm)浏览器
编辑C:\Users\video\AppData\Roaming\Python\Python34\site-packages\selenium\webdriver\phantomjs\service.py的文件cookie
找到:ide
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学习