安装浏览器
找到浏览器的版本号
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 --versiongeckodriver 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 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 其他界面相应的找到idinput.send_keys("hello world") # 向输入框内部输入hello worldinput.send_keys(Keys.ENTER) # 输入完之后回车time.sleep(10) # 等待页面加载完毕
finally:my_browser.close() # 关闭浏览器
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 版本的对应,比如
{"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,解压之后替换原来的。
geckodriver.exe --versiongeckodriver 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的,由于它没有提供更高的版本,我打算运行试一下。
chromedriver.exe --versionChromeDriver 114.0.5735.90 (386bc09e8f4f2e025eddae123f36f6263096ae49-refs/branch-heads/5735@{#1052})
from selenium import webdriver
driver = webdriver.Chrome()
报错了,但是从报错的信息中找到了有用信息
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
解压之后替换掉原来的
chromedriver.exe --versionChromeDriver 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:
- WebDriverManager (Java)
- WebDriver Manager (Python)
- WebDriver Manager Package (.NET)
- webdrivers gem (Ruby)
pip install webdriver-manager
# selenium 4
from selenium import webdriver
from selenium.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.firefox import GeckoDriverManagerdriver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))
三、Selenium IDE
在firefox浏览器上安装 Selenium IDE 扩展,在 Chrome 上也有,你可以在浏览器上创建和进行 selenium test,这比你写代码来调试方便的多,最后再整合到代码里去自动化运行。
Selenium IDE 文档
https://www.selenium.dev/selenium-ide/
https://www.selenium.dev/selenium-ide/docs/en/introduction/getting-started
以firefox为例
点击扩展中的 Selenium IDE,启动此工具
new 一个项目
名称:baidu
URL:http://www.baidu.com
于是就弹出了一个新窗口,打开了URL的页面,接这你在这个URL里面的所有操作都被记录了下来。
比如里面的id=kw
,它其实是分析了页面,因为这个输入框的id就是kw。
可以点击那个红色按钮Stop recording,然后点击Save project,比如我保存到 D:/selenium-test,你可以点击回放这个project。
有了这个project之后,一个很重要的东西就是 Command-line Runner
,
You can now run all of your Selenium IDE tests on any browser, in parallel, and on a Grid without needing to write any code.
Code Export
You can export either a test or suite of tests to WebDriver code by right-clicking on a test or a suite, selecting Export
, choosing your target language, and clicking Export
.
The exported code for Python pytest is built to work with Python 3, pytest 4.6.x, and the latest version of Selenium.
You should be able to take the exported JavaScript file and run it after installing these dependencies (e.g., with pip3 install
).
Here’s a sample requirements.txt
to help you get started.
pip install pytest, selenium
导出的代码如下
# Generated by Selenium IDE
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilitiesclass TestSearch():def setup_method(self, method):self.driver = webdriver.Firefox()self.vars = {}def teardown_method(self, method):self.driver.quit()def test_search(self):self.driver.get("https://www.baidu.com/")self.driver.set_window_size(550, 692)self.driver.find_element(By.ID, "kw").click()self.driver.find_element(By.ID, "kw").send_keys("hello world")self.driver.find_element(By.ID, "kw").send_keys(Keys.ENTER)time.sleep(1000)
pytest -s test/test_search.py
需要打开梯子