天天看点

python+selenium+webdriver+chromedriver

今天用到selenimu中的webdriver,在安装外selenium后,输入以下程序出现错误:"' chromedriver' executable needs to be in PATH":

from selenium import webdriver

chromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe" #配置一下环境变量也可

driver= webdriver.Chrome(chromedriver)

driver.get('http://www.baidu.com') 

查资料后是因为:

1.没有下载对应的chromedriver程序,下载浏览器对应版本的chromedriver版本

附录:链接chromedriver和浏览器对应版本: https://sites.google.com/a/chromium.org/chromedriver/home

2.将chromedriver程序加在项目目录下或者配置相关的环境变量也可

同样对应pahntomjs模块也是这样,报错信息‘WebDriverException: Message: 'phantomjs' executable needs to be in PATH. ’

对应下载phantomjs链接:http://phantomjs.org/download.html

也可以采用上述两种方法解决错误

browser=webdriver.PhantomJS(executable_path=r'C:\Users\lyh\Anaconda2\phantomjs-2.1.1-windows\bin\phantomjs.exe')