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()))
相关推荐
xuxie9938 分钟前
N11 ARM-irq
java·开发语言
wefly20171 小时前
从使用到原理,深度解析m3u8live.cn—— 基于 HLS.js 的 M3U8 在线播放器实现
java·开发语言·前端·javascript·ecmascript·php·m3u8
luanma1509802 小时前
PHP vs C++:编程语言终极对决
开发语言·c++·php
寂静or沉默2 小时前
2026最新Java岗位从P5-P7的成长面试进阶资源分享!
java·开发语言·面试
剑穗挂着新流苏3122 小时前
117_PyTorch 实战:利用训练好的模型进行单张图片验证
人工智能·python·深度学习
Lethehong2 小时前
Python Selenium全栈指南:从自动化入门到企业级实战
python·selenium·测试工具·自动化
kyriewen112 小时前
给浏览器画个圈:CSS contain 如何让页面从“卡成PPT”变“丝滑如德芙”
开发语言·前端·javascript·css·chrome·typescript·ecmascript
智算菩萨3 小时前
MP3音频编码原理深度解析与Python全参数调优实战:从心理声学模型到LAME编码器精细控制
android·python·音视频
娇娇yyyyyy3 小时前
QT编程(18): Qt QItemSelectionModel介绍
开发语言·qt
豆豆的java之旅3 小时前
软考中级软件设计师 数据结构详细知识点(含真题+练习题,可直接复习)
java·开发语言·数据结构