安装selenium与ChromeDriver的方法及遇到的错误。

安装selenium与ChromeDriver的方法及遇到的错误

一、安装selenium

方法一:在pycharm中安装

在Pycharm页面中File -> Settings -> Project:Name -> Project Interpreter ->   点击绿色‘+’ -> 搜索selenium安装

可以在 中设置pip镜像。

方法二:在控制台安装

键盘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.csdn.net/Eric_yong/article/details/86636902

截止2020年5月11日,新增

ChromeDriver版本支持的Chrome版本
v2.46v71-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的安装

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当使用 SeleniumChromeDriver 进行爬取时,有时会遇到浏览器错误,例如 ChromeDriver 版本与 Chrome 版本不匹配、ChromeDriver 可执行文件路径错误等。以下是一些常见的解决方法: 1. 确认 ChromeDriver 版本与 Chrome 版本匹配。可以通过在 Chrome 地址栏输入 `chrome://version/` 查看 Chrome 版本,然后到 ChromeDriver 官网下载相应版本的 ChromeDriver。 2. 指定 ChromeDriver 可执行文件路径。可以通过实例化 ChromeDriver 时传递 `webdriver.chrome.driver` 参数来指定 ChromeDriver 的可执行文件路径。例如: ```python from selenium import webdriver chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu') chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-dev-shm-usage') # 指定 ChromeDriver 可执行文件路径 driver_path = 'path/to/chromedriver' driver = webdriver.Chrome(executable_path=driver_path, chrome_options=chrome_options) # 访问页面 driver.get('https://www.xiaohongshu.com/explore/645874a800000000120338d6') ``` 3. 禁用 Chrome 浏览器的一些安全特性。可以通过在实例化 ChromeDriver 时传递 `--disable-web-security` 和 `--allow-running-insecure-content` 参数来禁用 Chrome 浏览器的一些安全特性。例如: ```python from selenium import webdriver chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu') chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-dev-shm-usage') chrome_options.add_argument('--disable-web-security') chrome_options.add_argument('--allow-running-insecure-content') driver = webdriver.Chrome(chrome_options=chrome_options) # 访问页面 driver.get('https://www.xiaohongshu.com/explore/645874a800000000120338d6') ``` 这些方法可能会绕过浏览器错误,但同时也会降低安全性,请谨慎使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值