selenium

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Selenium

Selenium

Selenium is an umbrella project for a range of tools and libraries that enable and support the automation of web browsers. It is the grandfather of browser automation and defined the W3C WebDriver standard.
Selenium是一个涵盖一系列工具和库的伞形项目,支持并实现Web浏览器自动化。它是浏览器自动化领域的先驱,定义了W3C WebDriver标准。

When to Use

适用场景

  • Legacy/Enterprise: Vast ecosystems and existing test suites.
  • Obscure Browsers: Need to test Internet Explorer (IE Mode) or specialized browsers.
  • Grid: Distributing tests across a massive farm of diverse OS/Browser combinations.
  • 遗留/企业系统:拥有庞大的生态系统和现有测试套件。
  • 小众浏览器:需要测试Internet Explorer(IE模式)或专用浏览器。
  • Grid部署:在由多种操作系统/浏览器组合构成的大规模集群上分布式运行测试。

Quick Start (Java)

快速开始(Java)

java
WebDriver driver = new ChromeDriver();
driver.get("https://selenium.dev");
WebElement element = driver.findElement(By.id("search"));
element.sendKeys("webdriver");
element.submit();
driver.quit();
java
WebDriver driver = new ChromeDriver();
driver.get("https://selenium.dev");
WebElement element = driver.findElement(By.id("search"));
element.sendKeys("webdriver");
element.submit();
driver.quit();

Core Concepts

核心概念

WebDriver

WebDriver

The API protocol that talks to the specific browser driver (chromedriver, geckodriver) which then controls the browser.
一种API协议,与特定的浏览器驱动(chromedriver、geckodriver)通信,进而控制浏览器。

Selenium Grid

Selenium Grid

Allows running tests on different machines against different browsers in parallel.
允许在不同机器上针对不同浏览器并行运行测试。

Page Object Model (POM)

Page Object Model (POM)

A design pattern where each UI page is a class. Tests interact with the class methods rather than raw elements.
一种设计模式,每个UI页面对应一个类。测试通过调用类方法而非直接操作原始元素来实现交互。

Best Practices (2025)

2025年最佳实践

Do:
  • Use Explicit Waits:
    WebDriverWait(driver).until(ExpectedConditions....)
    .
  • Use Headless Mode: For faster CI execution (
    ChromeOptions.addArguments("--headless")
    ).
  • Migrate to W3C: Ensure you are using W3C compliant capabilities.
Don't:
  • Don't use Thread.sleep: It slows down tests and is flaky.
  • Don't mix Logic and Tests: Strictly follow Page Object Model (POM) to keep tests readable.
建议
  • 使用显式等待
    WebDriverWait(driver).until(ExpectedConditions....)
  • 使用无头模式:以加快CI执行速度(
    ChromeOptions.addArguments("--headless")
    )。
  • 迁移至W3C标准:确保使用符合W3C规范的功能配置。
避免
  • 不要使用Thread.sleep:会减慢测试速度且不稳定。
  • 不要混合逻辑与测试:严格遵循Page Object Model (POM)以保持测试可读性。

References

参考资料