使用selenium启动浏览器时,进行脚本演练时出现报错如下:
Traceback (most recent call last):
File "D:\jd233.py", line 6, in <module>
driver = webdriver.Chrome("23333")
File "D:\Python\Python36-32\lib\site-packages\selenium-3.4.3-py3.6.egg\selenium\webdriver\chrome\webdriver.py", line 62, in __init__
self.service.start()
File "D:\Python\Python36-32\lib\site-packages\selenium-3.4.3-py3.6.egg\selenium\webdriver\common\service.py", line 96, in start
self.assert_process_still_running()
File "D:\Python\Python36-32\lib\site-packages\selenium-3.4.3-py3.6.egg\selenium\webdriver\common\service.py", line 109, in assert_process_still_running
% (self.path, return_code)
selenium.common.exceptions.WebDriverException: Message: Service chromedriver.exe unexpectedly exited. Status code was: 1
这时候要安装浏览器驱动器(要区分不同的浏览器驱动)
以下介绍火狐和谷歌的安装方法:
1、下载Geckodriver(下载完毕解压备用)
下载地址:https://github.com/mozilla/geckodriver/releases
1.下载DhromeDriver:
下载网址:http://npm.taobao.org/mirrors/chromedriver/2.41/
1.若是没有将python固定变量环境,需要指定驱动器位置地址。
以下这句话有问题(未设置变量环境):
driver = webdriver.Chrome("chromedriver.exe")
加载chromedriver的时候,必须指明路径,类似下面
driver = webdriver.Chrome("D:\Python\chromedriver_win32\chromedriver.exe")
2.火狐驱动提示:NotADirectoryError: [WinError 267] 目录名称无效。: 'D:\tools\Program Files\Driver\geckodriver.exe’
from selenium import webdriver
FFpath = r'D:\tools\Program Files\Driver\geckodriver.exe'
driver = webdriver.Firefox(FFpath)
原因是WebDirver Firefox方法调用方式不对:改成如下方式:
from selenium import webdriver
#这里要指定executeable_path参数来传参数
FFDriverServer = r'D:\tools\Program Files\Driver\geckodriver.exe'
driver = webdriver.Firefox(executable_path=FFDriverServer)