安装浏览器
找到浏览器的版本号
chrome 版本 130.0.6723.92(正式版本) (64 位)
firfox 116.0.3 (64 位),但是后面运行的时候又自动更新到了 127.0.0.8923
安装selenium
> 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 目的在于web testing。
selenium 文档
https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location
https://github.com/SergeyPirogov/webdriver_manager
安装浏览器驱动的方式有两种
一、手动下载
浏览器 | 支持的操作系统 | 维护者 | 下载 | 问题追溯 |
---|---|---|---|---|
Chromium/Chrome | Windows/macOS/Linux | 下载 | 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
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/.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import</