Selenium 笔记¶
约 1427 个字 163 行代码 预计阅读时间 7 分钟
View Times
介绍 / Introduction¶
Selenium 是一个用于自动化 Web 应用程序测试的工具。它提供了一系列的 API 来控制浏览器的行为。 Selenium is a tool for automating web application testing. It provides a set of APIs to control the behavior of the browser.
Selenium 4 是 Selenium 的最新版本,带来了许多新特性和改进。以下是一些主要更新: Selenium 4 is the latest version of Selenium, bringing many new features and improvements. Here are some major updates:
Selenium 4 新特性 / New Features in Selenium 4¶
-
W3C WebDriver 标准: Selenium 4 完全支持 W3C WebDriver 标准,这使得浏览器驱动程序的兼容性更好。 W3C WebDriver Standard: Selenium 4 fully supports the W3C WebDriver standard, which improves compatibility with browser drivers.
-
相对定位器: 新增了相对定位器,可以更方便地定位元素。 Relative Locators: New relative locators make it easier to locate elements.
-
改进的 DevTools 支持: Selenium 4 提供了对 Chrome DevTools 协议的更好支持,可以更方便地进行调试和性能分析。 Improved DevTools Support: Selenium 4 provides better support for the Chrome DevTools Protocol, making it easier to debug and analyze performance.
-
新命令: Selenium 4 引入了一些新命令,例如
getElementRect
和executeCdpCommand
。 New Commands: Selenium 4 introduces new commands such asgetElementRect
andexecuteCdpCommand
. -
改进的文档: Selenium 4 的文档更加详细和易于理解。 Improved Documentation: The documentation for Selenium 4 is more detailed and easier to understand.
注意事项 / Considerations¶
-
浏览器驱动兼容性: 确保使用与 Selenium 4 兼容的浏览器驱动程序。 Browser Driver Compatibility: Ensure that you use browser drivers compatible with Selenium 4.
-
API 变化: 一些 API 在 Selenium 4 中发生了变化,升级时需要注意。 API Changes: Some APIs have changed in Selenium 4, so be aware of these changes when upgrading.
-
性能优化: 利用 Selenium 4 的新特性和改进,可以更好地优化测试性能。 Performance Optimization: Utilize the new features and improvements in Selenium 4 to better optimize test performance.
安装 / Installation¶
要安装 Selenium,可以使用 pip: To install Selenium, you can use pip:
Bash | |
---|---|
此外,你还需要下载相应的浏览器驱动程序,并将其路径添加到系统环境变量中。 Additionally, you need to download the corresponding browser driver and add its path to the system environment variables.
快速开始 / Quick Start¶
以下是一个简单的示例,展示了如何使用 Selenium 打开一个网页并获取其标题。 Here is a simple example showing how to use Selenium to open a webpage and get its title.
浏览器驱动 / Browser Drivers¶
Selenium 需要一个浏览器驱动程序来与浏览器进行通信。以下是一些常用的浏览器驱动程序及其下载链接:
Selenium requires a browser driver to communicate with the browser. Here are some commonly used browser drivers and their download links:
- ChromeDriver: https://sites.google.com/a/chromium.org/chromedriver/
- GeckoDriver (Firefox): https://github.com/mozilla/geckodriver/releases
- EdgeDriver: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
下载后,将驱动程序解压到一个目录,并将该目录添加到系统的 PATH 环境变量中。
After downloading, extract the driver to a directory and add that directory to the system PATH environment variable.
常用操作 / Common Operations¶
查找元素 / Finding Elements¶
Selenium 提供了多种方法来查找页面上的元素,例如 find_element_by_id
、find_element_by_name
、find_element_by_xpath
等。
Selenium provides several methods to find elements on a page, such as find_element_by_id
, find_element_by_name
, find_element_by_xpath
, etc.
以下是一些常用的查找元素的方法及其示例,使用百度和哔哩哔哩网站作为示例。 Here are some commonly used methods to find elements and their examples, using Baidu and Bilibili websites as examples.
通过 ID 查找元素 / Find Element by ID¶
Python | |
---|---|
通过名称查找元素 / Find Element by Name¶
Python | |
---|---|
通过 XPath 查找元素 / Find Element by XPath¶
通过 CSS 选择器查找元素 / Find Element by CSS Selector¶
交互操作 / Interacting with Elements¶
可以对找到的元素进行各种操作,例如点击、输入文本等。 You can perform various actions on the found elements, such as clicking, entering text, etc.
等待 / Waiting¶
在自动化测试中,等待是一个重要的部分。Selenium 提供了显式等待和隐式等待两种方式。 Waiting is an important part of automated testing. Selenium provides both explicit and implicit waits.
处理弹窗 / Handling Alerts¶
在自动化测试中,有时需要处理浏览器弹窗。Selenium 提供了处理弹窗的 API。 Sometimes you need to handle browser alerts in automated testing. Selenium provides APIs to handle alerts.
截图 / Taking Screenshots¶
Selenium 还提供了截图功能,可以在测试过程中保存页面截图。 Selenium also provides a screenshot feature to save page screenshots during testing.
Python | |
---|---|
XPath 语法 / XPath Syntax¶
XPath 是一种用于在 XML 文档中查找节点的语言。Selenium 支持使用 XPath 查找页面元素。 XPath is a language used for finding nodes in an XML document. Selenium supports using XPath to find elements on a page.
基本语法 / Basic Syntax¶
//tagname[@attribute='value']
:选取所有具有指定属性和值的元素。//tagname[text()='text']
:选取所有具有指定文本的元素。//tagname[contains(@attribute, 'value')]
:选取所有属性包含指定值的元素。//tagname[contains(text(), 'text')]
:选取所有文本包含指定值的元素。
学习 XPath / Learning XPath¶
学习 XPath 语法可以帮助你更好地使用 Selenium 查找页面元素。以下是一些学习资源:
Learning XPath syntax can help you better use Selenium to find page elements. Here are some learning resources:
实际使用 / Practical Usage¶
在实际使用中,XPath 可以帮助你定位复杂的页面元素。以下是一些实际使用的示例: In practical usage, XPath can help you locate complex page elements. Here are some practical examples:
示例 / Examples¶
进阶示例 / Advanced Examples¶
表单处理 / Handling Forms¶
以下示例展示了如何使用 Selenium 填写并提交表单。 The following example shows how to use Selenium to fill out and submit a form.
处理多窗口 / Handling Multiple Windows¶
以下示例展示了如何使用 Selenium 处理多窗口。 The following example shows how to use Selenium to handle multiple windows.
结束 / Conclusion¶
更多详细信息,请参考 Selenium 官方文档. For more detailed information, please refer to the Selenium official documentation.