安装selenium与ChromeDriver的方法及遇到的错误。
安装selenium与ChromeDriver的方法及遇到的错误
一、安装selenium
方法一:在pycharm中安装
在Pycharm页面中File -> Settings -> Project:Name -> Project Interpreter -> 点击绿色‘+’ -> 搜索selenium安装
可以在 中设置pip镜像。
- 清华:https://pypi.tuna.tsinghua.edu.cn/simple
- 阿里云:http://mirrors.aliyun.com/pypi/simple/
- 中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
- 华中理工大学:http://pypi.hustunique.com/
- 山东理工大学:http://pypi.sdutlinux.org/
- 豆瓣:http://pypi.douban.com/simple/
方法二:在控制台安装
键盘win+R打开搜索框,输入cmd 打开控制台 -> 输入 pip install selenium 安装
可以在输入时添加 -i 'pip镜像网址' 如:pip install selenium -i http://mirrors.aliyun.com/pypi/simple/
二、下载及使用ChromeDriver
1、下载
官方网址:http://npm.taobao.org/mirrors/chromedriver/ 下载
注:必须根据当前chrome版本安装相应的版本的ChromeDriver
否则会报错:
selenium.common.exceptions.WebDriverException: Message: unknown error: Runtime.executionContextCreated has invalid 'context' {"auxData":{"frameId":"FBDDD78438129FCE59A700FE9C518C72","isDefault":true,"type":"default"},"id":1,"name":"","origin":"://"} (Session info: chrome=73.0.3683.86)
(Driver info: chromedriver=2.9.248315,platform=Windows NT 6.3 x86_64)
ChromeDriver支持的Chrome版本,具体参考:https://blog.****.net/Eric_yong/article/details/86636902
截止2020年5月11日,新增
ChromeDriver版本 | 支持的Chrome版本 |
---|---|
v2.46 | v71-73 |
2、使用
使用ChromeDriver需要将下载的文件,解压放到python目录下
PS:我没有设置环境变量,下面的报错提示,只将文件放到了指定目录下。(可能之前在别的项目里有过设置)
否则会报错:
Traceback (most recent call last):
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
stdin=PIPE)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python36\Lib\subprocess.py", line 709, in __init__restore_signals, start_new_session)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python36\Lib\subprocess.py", line 997, in _execute_child startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/PyCharm/python爬虫/20-05-11.py", line 3, in <module>
dricer = webdriver.Chrome()
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
self.service.start()
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\common\service.py", line 83, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
最后,完成整个selenium及ChromeDriver的安装