首页 > 编程 > Python > 正文

selenium 安装与chromedriver安装的方法步骤

2019-11-25 12:46:41
字体:
来源:转载
供稿:网友

安装

selenium可以直接可以用pip安装。

pip install selenium

chromedriver的安装一定要与Chrome的版本一致,不然就不起作用(不要问我是怎么知道的)。

有两个下载地址:

1、http://chromedriver.storage.googleapis.com/index.html

2、https://npm.taobao.org/mirrors/chromedriver/

或者本地下载https://www.VeVB.COm/softs/538241.html

当然,你首先需要查看你的Chrome版本,在浏览器中输入chrome://version/

例如我的版本是72.0.3626,所以下载

配置

解压压缩包,找到chromedriver.exe复制到chrome的安装目录(其实也可以随便放一个文件夹)。复制chromedriver.exe文件的路径并加入到电脑的环境变量中去。具体的:


进入环境变量编辑界面,添加到用户变量即可,双击PATH,将你的文件位置(C:/Program Files (x86)/Google/Chrome/Application/)添加到后面。

完成后在cmd下输入chromedriver验证是否安装成功:

测试

未配置环境也可以,例如:

from selenium import webdriverimport timedef 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 webdriverimport timedef main(): b = webdriver.Chrome() b.get('https://www.baidu.com') time.sleep(5) b.quit()if __name__ == '__main__': main()

如果运行时提示

很可能是chromedriver的版本不对(不要问我怎么知道的)。

参考链接:

1、https://www.VeVB.COm/article/162900.htm

2、https://www.VeVB.COm/article/162903.htm

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表