webdriver
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseWebDriver
WebDriver
WebDriver is the W3C standard protocol for controlling web browsers. It is the underlying technology behind Selenium, Appium, WebdriverIO, and more. Even if you use a high-level tool, understanding WebDriver helps debug low-level issues.
WebDriver是用于控制网页浏览器的W3C标准协议。它是Selenium、Appium、WebdriverIO等工具背后的底层技术。即使你使用的是高级工具,理解WebDriver也有助于调试底层问题。
When to Use
适用场景
- Protocol Knowledge: Understanding why happens.
stale element reference - Custom Integration: Building your own test runner or browser automation tool.
- WebdriverIO: A popular Node.js implementation of the WebDriver protocol (often used over raw Selenium).
- 协议认知:理解(过时元素引用)错误发生的原因。
stale element reference - 自定义集成:构建自定义测试运行器或浏览器自动化工具。
- WebdriverIO:WebDriver协议的一款热门Node.js实现(通常优先于原生Selenium使用)。
Quick Start (WebdriverIO)
快速入门(WebdriverIO)
javascript
import { remote } from "webdriverio";
const browser = await remote({
capabilities: {
browserName: "chrome",
"goog:chromeOptions": { args: ["headless", "disable-gpu"] },
},
});
await browser.url("https://webdriver.io");
const title = await browser.getTitle();
console.log(title); // outputs: "WebdriverIO · Next-gen browser and mobile automation test framework for Node.js"
await browser.deleteSession();javascript
import { remote } from "webdriverio";
const browser = await remote({
capabilities: {
browserName: "chrome",
"goog:chromeOptions": { args: ["headless", "disable-gpu"] },
},
});
await browser.url("https://webdriver.io");
const title = await browser.getTitle();
console.log(title); // outputs: "WebdriverIO · Next-gen browser and mobile automation test framework for Node.js"
await browser.deleteSession();Core Concepts
核心概念
Client-Server Architecture
客户端-服务器架构
- Client: Your test script (Node/Java/Python).
- Server: The Browser Driver (chromedriver, geckodriver) or Grid.
- Protocol: REST-ish JSON commands ().
POST /session/:id/element
- 客户端:你的测试脚本(Node/Java/Python)。
- 服务器:浏览器驱动(chromedriver、geckodriver)或Grid。
- 协议:类REST的JSON命令()。
POST /session/:id/element
Stale Element Reference
过时元素引用
A common error. You got a reference to a DOM element (ID: 123), but the page refreshed or JS updated the DOM. ID 123 is gone. You must find the element again.
这是一个常见错误。你获取了某个DOM元素的引用(ID: 123),但页面刷新或JS更新了DOM,ID 123的元素已不存在,你必须重新查找该元素。
Best Practices (2025)
2025年最佳实践
Do:
- Use WebdriverIO (WDIO): If you want to use WebDriver in Node.js. It wraps the low-level protocol in a nice, synchronous-looking API.
- Understand the network: WebDriver is chatty (many HTTP requests). Running tests "remote" (e.g., SauceLabs) is slower than local due to latency.
Don't:
- Don't mix protocols: Don't confuse CDP (Puppeteer/Playwright) with WebDriver. They work differently.
建议:
- 使用WebdriverIO (WDIO):如果你想在Node.js中使用WebDriver,推荐使用WebdriverIO。它将底层协议封装为简洁、类似同步的API。
- 了解网络机制:WebDriver的通信较为频繁(包含大量HTTP请求)。在远程环境(如SauceLabs)运行测试会因网络延迟比本地测试更慢。
不建议:
- 不要混用协议:不要混淆CDP(Puppeteer/Playwright使用的协议)与WebDriver,二者工作机制不同。