04 phantomjs安装步骤
Windows环境:
1、下载
http://phantomjs.org/download.html
2、解压
phantomjs-2.1.1-windows.zip
3、配置环境变量
将解压的bin目录的路径配置到环境变量中去,
打开我的电脑右键选择属性,
双击Path
我是放在这里,
点击新建
点击确定,然后在命令终端执行看下版本,安装好了。如果在打开命令终端打开的状态下安装的phantomjs,请关闭命令终端后重新打开。
pycharm中进行检验成功则可以在pycharm中运行。如果在打开pycharm的状态下安装phantomjs,请关闭pycharm后重新打开。
最后就可以执行了,就截图就出来了
图中警告表示是 Selenium 已经弃用 PhantomJS ,请使用火狐或者谷歌*面浏览器。 可以忽略。
如果想消除警告,可以降低Selenium 为3.0以下版本,或者使用火狐或者谷歌*面浏览器。
chromedriver安装:
安装selenium
selenium可以直接可以用pip安装。
pip install selenium
安装chromedriver
下载
chromedriver的版本一定要与Chrome的版本一致,不然就不起作用。
有两个下载地址:
1、http://chromedriver.storage.googleapis.com/index.html
2、https://npm.taobao.org/mirrors/chromedriver/
当然,你首先需要查看你的Chrome版本,在浏览器中输入chrome://version/
例如我的版本是72.0.3626,所以下载
配置
解压压缩包,找到chromedriver.exe复制到chrome的安装目录(其实也可以随便放一个文件夹)。复制chromedriver.exe文件的路径并加入到电脑的环境变量中去。具体的:
进入环境变量编辑界面,添加到用户变量即可,双击PATH,将你的文件位置(C:\Program Files (x86)\Google\Chrome\Application\)添加到后面。
完成后在cmd下输入chromedriver验证是否安装成功:
测试
未配置环境也可以,例如:
from selenium import webdriver import time def main(): chrome_driver = 'C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe' #chromedriver的文件位置 b = webdriver.Chrome(executable_path = chrome_driver) b.get('https://www.google.com') time.sleep(5) b.quit() if __name__ == '__main__': main()
已配置环境变量时
、
from selenium import webdriver import time def main(): b = webdriver.Chrome() b.get('https://www.baidu.com') time.sleep(5) b.quit() if __name__ == '__main__': main()
如果运行时提示
很可能是chromedriver的版本不对(不要问我怎么知道的)。