1. Selenium 介绍

Selenium 是一个用于Web应用程序测试的工具。Selenium 测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7, 8, 9, 10, 11)Mozilla FirefoxSafariGoogle ChromeOperaEdge 等。这个工具的主要功能包括:测试与浏览器的兼容性——测试应用程序看是否能够很好得工作在不同浏览器和操作系统之上。测试系统功能——创建回归测试检验软件功能和用户需求。支持自动录制动作和自动生成 .Net、Java、Perl 等不同语言的测试脚本。

Selenium 测试直接在浏览器中运行,就像真实用户所做的一样。Selenium 测试可以在 Windows、Linux 和Macintosh 上的 Internet Explorer、Chrome 和 Firefox 中运行。其他测试工具都不能覆盖如此多的平台。使用Selenium和在浏览器中运行测试还有很多其他好处。

2. Selenium 使用

https://www.selenium.dev/selenium/docs/api/py/#

2.1 安装

Selenium 功能强大,但它的安装步骤,非常简单,只需要 2 步:

第 1 步,安装 selenium-python 依赖库

Selenium 支持的 Python 3.8+ 版本

如果您的系统上有pip,则只需安装或升级 Python 绑定即可:

# 安装依赖库
➜  ~ pip install -U selenium
Defaulting to user installation because normal site-packages is not writeable
Collecting selenium
  Obtaining dependency information for selenium from https://files.pythonhosted.org/packages/fc/df/a8972c41279fc9e9404cad87bc1f4d6d3d824b84c5c072dca0e986a89680/selenium-4.14.0-py3-none-any.whl.metadata
  Downloading selenium-4.14.0-py3-none-any.whl.metadata (6.9 kB)
Requirement already satisfied: urllib3[socks]<3,>=1.26 in c:\programdata\anaconda3\lib\site-packages (from selenium) (1.26.16)
Collecting trio~=0.17 (from selenium)
  Obtaining dependency information for trio~=0.17 from https://files.pythonhosted.org/packages/a3/dd/b61fa61b186d3267ef3903048fbee29132963ae762fb70b08d4a3cd6f7aa/trio-0.22.2-py3-none-any.whl.metadata
  Downloading trio-0.22.2-py3-none-any.whl.metadata (4.7 kB)
Collecting trio-websocket~=0.9 (from selenium)
  Obtaining dependency information for trio-websocket~=0.9 from https://files.pythonhosted.org/packages/48/be/a9ae5f50cad5b6f85bd2574c2c923730098530096e170c1ce7452394d7aa/trio_websocket-0.11.1-py3-none-any.whl.metadata
  Downloading trio_websocket-0.11.1-py3-none-any.whl.metadata (4.7 kB)
Requirement already satisfied: certifi>=2021.10.8 in c:\programdata\anaconda3\lib\site-packages (from selenium) (2023.7.22)
Requirement already satisfied: attrs>=20.1.0 in c:\programdata\anaconda3\lib\site-packages (from trio~=0.17->selenium) (22.1.0)
Requirement already satisfied: sortedcontainers in c:\programdata\anaconda3\lib\site-packages (from trio~=0.17->selenium) (2.4.0)
Requirement already satisfied: idna in c:\programdata\anaconda3\lib\site-packages (from trio~=0.17->selenium) (3.4)
Collecting outcome (from trio~=0.17->selenium)
  Obtaining dependency information for outcome from https://files.pythonhosted.org/packages/55/8b/5ab7257531a5d830fc8000c476e63c935488d74609b50f9384a643ec0a62/outcome-1.3.0.post0-py2.py3-none-any.whl.metadata
  Downloading outcome-1.3.0.post0-py2.py3-none-any.whl.metadata (2.6 kB)
Requirement already satisfied: sniffio in c:\programdata\anaconda3\lib\site-packages (from trio~=0.17->selenium) (1.2.0)
Requirement already satisfied: cffi>=1.14 in c:\programdata\anaconda3\lib\site-packages (from trio~=0.17->selenium) (1.15.1)
Collecting wsproto>=0.14 (from trio-websocket~=0.9->selenium)
  Downloading wsproto-1.2.0-py3-none-any.whl (24 kB)
Requirement already satisfied: PySocks!=1.5.7,<2.0,>=1.5.6 in c:\programdata\anaconda3\lib\site-packages (from urllib3[socks]<3,>=1.26->selenium) (1.7.1)
Requirement already satisfied: pycparser in c:\programdata\anaconda3\lib\site-packages (from cffi>=1.14->trio~=0.17->selenium) (2.21)
Collecting h11<1,>=0.9.0 (from wsproto>=0.14->trio-websocket~=0.9->selenium)
  Downloading h11-0.14.0-py3-none-any.whl (58 kB)
     ---------------------------------------- 58.3/58.3 kB 1.5 MB/s eta 0:00:00
Downloading selenium-4.14.0-py3-none-any.whl (9.9 MB)
   ---------------------------------------- 9.9/9.9 MB 9.9 MB/s eta 0:00:00
Downloading trio-0.22.2-py3-none-any.whl (400 kB)
   ---------------------------------------- 400.2/400.2 kB 12.6 MB/s eta 0:00:00
Downloading trio_websocket-0.11.1-py3-none-any.whl (17 kB)
Downloading outcome-1.3.0.post0-py2.py3-none-any.whl (10 kB)
Installing collected packages: outcome, h11, wsproto, trio, trio-websocket, selenium
Successfully installed h11-0.14.0 outcome-1.3.0.post0 selenium-4.14.0 trio-0.22.2 trio-websocket-0.11.1 wsproto-1.2.0

或者,您可以从 PyPI 下载源发行版 https://pypi.org/project/selenium/#files,将其解压并运行:

python setup.py install
第 2 步,安装浏览器驱动

Selenium 需要一个驱动程序来与所选浏览器交互。例如,Firefox 需要 geckodriver,需要安装它才能运行以下示例。确保它在您的PATH中,例如,将其放在 /usr/bin/usr/local/bin 中。

如果不遵守此步骤,将会出现错误 selenium.common.exceptions.WebDriverException: Message: ‘geckodriver’ executable needs to be in PATH.

其他受支持的浏览器将有自己的驱动程序可用。下面是一些更流行的浏览器驱动程序的链接。

3. 实操演示

3.1 示例-0
  • 打开新的火狐浏览器
  • 加载给定 URL 的页面
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://www.baidu.com')
3.2 示例-1
  • 打开新的火狐浏览器
  • 加载雅虎主页
  • 搜索“seleniumhq”
  • 关闭浏览器
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox()

browser.get('https://www.baidu.com')
assert '百度一下,你就知道' in browser.title

elem = browser.find_element(By.ID, 'kw')  # Find the search box
elem.send_keys('seleniumhq' + Keys.RETURN)

browser.quit()
3.3 示例-2

Selenium WebDriver 通常用作测试 Web 应用程序的基础。下面是一个使用 Python 标准单元测试库的简单示例:

import unittest
from selenium import webdriver

class BaiduTestCase(unittest.TestCase):

    def setUp(self):
        self.browser = webdriver.Firefox()
        self.addCleanup(self.browser.quit)

    def test_page_title(self):
        self.browser.get('https://www.baidu.com')
        self.assertIn('百度一下,你就知道', self.browser.title)

if __name__ == '__main__':
    unittest.main(verbosity=2)

Selenium 服务器(可选)

对于普通的 WebDriver 脚本(非远程),不需要 Java 服务器。

但是,要使用 Selenium Webdriver Remote ,您还需要运行 Selenium 服务器。服务器需要 Java 运行时环境 (JRE)。

单独下载服务器,来自: https://www.selenium.dev/downloads/

从命令行运行服务器:

java -jar selenium-server-4.14.0.jar

然后运行 Python 客户端脚本。

作者:Jeebiz  创建时间:2023-10-27 19:23
最后编辑:Jeebiz  更新时间:2024-06-06 00:29