python安装selenium,geckodriver,chromedriver

安装浏览器

找到浏览器的版本号

chrome 版本 130.0.6723.92(正式版本) (64 位)

firfox 116.0.3 (64 位),但是后面运行的时候又自动更新到了 127.0.0.8923

安装selenium

python 复制代码
> pip install selenium
> pip show selenium
Name: selenium
Version: 4.26.1
Summary: Official Python bindings for Selenium WebDriver
Home-page: https://www.selenium.dev
Author:
Author-email:
License: Apache 2.0
Location: d:\programdata\anaconda3\lib\site-packages
Requires: certifi, trio, trio-websocket, typing_extensions, urllib3, websocket-client
Required-by:

其原理是 selenium 通过浏览器驱动向浏览器发送指令。

selenium 文档

https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location

https://github.com/SergeyPirogov/webdriver_manager

安装浏览器驱动的方式有两种

一、手动下载
浏览器 支持的操作系统 维护者 下载 问题追溯
Chromium/Chrome Windows/macOS/Linux Google 下载 Issues
Firefox Windows/macOS/Linux Mozilla 下载 Issues
Edge Windows/macOS/Linux Microsoft 下载 Issues
Internet Explorer Windows Selenium Project 下载 Issues
Safari macOS High Sierra and newer Apple 内置 Issues

备注:Opera驱动不再适用于Selenium的最新功能,目前官方不支持。

将下载的压缩包解压,然后添加到环境变量, 确保可以直接使用命令访问到

firefox驱动下载

firefox的驱动是geckodriver,仓库地址为:https://github.com/mozilla/geckodriver

下载 https://github.com/mozilla/geckodriver/releases/download/v0.34.0/geckodriver-v0.34.0-win64.zip

shell 复制代码
geckodriver.exe --version

geckodriver 0.34.0 (c44f0d09630a 2024-01-02 15:36 +0000)

The source code of this program is available from
testing/geckodriver in https://hg.mozilla.org/mozilla-central.

This program is subject to the terms of the Mozilla Public License 2.0.
You can obtain a copy of the license at https://mozilla.org/MPL/2.0/.
python 复制代码
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
import time
my_browser = webdriver.Firefox()
try:
    my_browser.get("https://www.baidu.com")
    input = my_browser.find_element(By.ID, "kw") # html代码中输入框的id是kw 其他界面相应的找到id
    input.send_keys("hello world")  # 向输入框内部输入hello world
    input.send_keys(Keys.ENTER)  # 输入完之后回车
    time.sleep(10)  # 等待页面加载完毕
finally:
    my_browser.close()  # 关闭浏览器
bash 复制代码
Problem reading geckodriver versions: error sending request for url (https://raw.githubusercontent.com/SeleniumHQ/selenium/trunk/common/geckodriver/geckodriver-support.json). Using latest geckodriver version
The geckodriver version (0.34.0) detected in PATH at D:\geckodriver-v0.34.0-win64\geckodriver.exe might not be compatible with the detected firefox version (127.0.0.8923
); currently, geckodriver 0.35.0 is recommended for firefox 127.*, so it is advised to delete the driver in PATH and retry

但是 firefox的确是启动了。

但是从错误信息中我们发现了重要信息,把Json文件下载下来,可以看到里面列出了 geckodriver 与 firefox 版本的对应,比如

json 复制代码
{
  "geckodriver-releases": [
    {
      "geckodriver-version": "0.35.0",
      "min-firefox-version": 115
    },
    {
      "geckodriver-version": "0.34.0",
      "min-firefox-version": 115
    },
    {
      "geckodriver-version": "0.33.0",
      "min-firefox-version": 102,
      "max-firefox-version": 120
    },
    ......

的确,firefox-127.x 需要使用 geckodriver-0.35.0,这也是错误信息中提到的。但是在运行的时候是需要开梯子的,不然它拉取不到这个json文件。于是我们下载 geckodriver-0.35.0-win64,解压之后替换原来的。

bash 复制代码
geckodriver.exe --version

geckodriver 0.35.0 (9f0a0036bea4 2024-08-03 07:11 +0000)

The source code of this program is available from
testing/geckodriver in https://hg.mozilla.org/mozilla-central.

This program is subject to the terms of the Mozilla Public License 2.0.
You can obtain a copy of the license at https://mozilla.org/MPL/2.0/.

再次运行代码,没有任何报错,且浏览器行为正常。

chrome驱动下载

1、https://registry.npmmirror.com/binary.html?path=chromedriver

2、https://chromedriver.chromium.org/downloads

3、https://developer.chrome.com/docs/chromedriver/downloads?hl=zh-cn

文档中给的下载链接(2和3)没有找到下载按钮,而且最高只有114开头的版本,于是我使用 链接1 下载了一个,版本号为 114.0.5735.90,还是win32的,由于它没有提供更高的版本,我打算运行试一下。

bash 复制代码
chromedriver.exe --version

ChromeDriver 114.0.5735.90 (386bc09e8f4f2e025eddae123f36f6263096ae49-refs/branch-heads/5735@{#1052})
python 复制代码
from selenium import webdriver
driver = webdriver.Chrome()

报错了,但是从报错的信息中找到了有用信息

bash 复制代码
Exception managing chrome: error sending request for url (https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json)
The chromedriver version (114.0.5735.90) detected in PATH at D:\geckodriver-v0.34.0-win64\chromedriver.exe might not be compatible with the detected chrome version (130.
0.6723.92); currently, chromedriver  is recommended for chrome 130.*, so it is advised to delete the driver in PATH and retry
Error sending stats to Plausible: error sending request for url (https://plausible.io/api/event)

把这个json文件下载下来,里面包含了所有版本的chrone对应的chromedriver版本以及下载地址。没有找到 130.0.6723.92,于是选择了 https://storage.googleapis.com/chrome-for-testing-public/130.0.6723.91/win64/chromedriver-win64.zip

解压之后替换掉原来的

bash 复制代码
chromedriver.exe --version

ChromeDriver 130.0.6723.91 (53ac076783696778ecc8f360ea31765c29c240ad-refs/branch-heads/6723@{#1517})

将上面代码中的 Firefox改成 Chrome 即可,运行

二、使用 Selenium Manager (未测试)

Before Selenium managed drivers itself, other projects were created to do so for you.

If you can't use Selenium Manager because you are using an older version of Selenium (please upgrade), or need an advanced feature not yet implemented by Selenium Manager, you might try one of these tools to keep your drivers automatically updated:

python 复制代码
pip install webdriver-manager
python 复制代码
# selenium 4
from selenium import webdriver
from selenium.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.firefox import GeckoDriverManager

driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))
相关推荐
默_笙5 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
小羊没烦恼!5 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
qq_426003965 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫5 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技5 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
伞伞悦读5 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时5 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
C语言小火车5 天前
C/C++ 为什么需要编译器?
开发语言·c++
奇思妙想聪明勤奋的小羊5 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型
lpfasd1235 天前
2026年第38周GitHub趋势周报
python·科技·github