Playwright 比 Selenium 更适合新项目,因其原生多浏览器支持、自动等待、沙箱隔离、文本模糊定位及惰性求值等特性显著提升稳定性与开发效率。

Playwright 比 Selenium 更适合新项目
除非你手上有大量现成的 Selenium 脚本要维护,否则现在写 Web 自动化测试,Playwright 是更省心的选择。它原生支持多浏览器(Chromium、Firefox、WebKit),自动等待 DOM 就绪,不用手动写 WebDriverWait + expected_conditions,而且默认开沙箱、无头模式稳定,不依赖系统级浏览器驱动。
常见错误现象:ElementNotInteractableError 或 TimeoutError 在 Selenium 里高频出现,往往是因为显式等待没写对或隐式等待和显式混用;而 Playwright 的 page.locator() + click() 默认带自动等待和可点击性校验,多数情况直接写就跑通。
- 使用场景:CI/CD 流水线中做冒烟测试、登录流程回归、表单提交验证
- 参数差异:Selenium 的
find_element(By.XPATH, "...")需精确匹配,Playwright 的page.locator("text=提交")支持文本模糊定位,容错更强 - 性能影响:Playwright 启动浏览器实例更快,尤其在并行执行多个测试时,
browser.new_context()开销远低于 Selenium 的webdriver.Chrome()
Selenium 里 find_element 和 find_elements 别乱混用
这是最常踩的坑:用 find_elements 返回空列表,却当成单个元素去调 .click(),结果报 AttributeError: 'list' object has no attribute 'click';反过来,用 find_element 找不到元素直接抛 NoSuchElementException,没包 try/except 就崩。
实操建议:
立即学习“Python免费学习笔记(深入)”;
- 明确你要操作的是「一个」还是「一批」:要等某个按钮出现再点,用
find_element;要遍历所有同类型卡片取标题,用find_elements - 别依赖
time.sleep()等页面加载,改用WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.ID, "submit"))) - 注意作用域:Selenium 的
find_element在父元素上调用时,是相对查找,但很多人误以为是全局,结果定位失败
Playwright 中 locator 不等于 element_handle
刚上手容易把 page.locator("button") 当成“已经找到的元素”,其实它只是个查询对象,不触发实际查找,直到你调 .click()、.text_content() 这类动作才真正执行。这带来两个关键影响:
- 性能友好:链式调用如
page.locator("form").locator("button").click()是惰性求值,不会中间多查一次 - 但也会掩盖问题:比如页面已跳转,你还拿着旧
locator去操作,会报TimeoutError—— 因为它还在等原页面里的元素 - 需要强制获取实时句柄?用
locator.element_handle(),但一般没必要,直接操作 locator 更安全
本地调试时别忽略浏览器上下文隔离
Playwright 默认每个 browser.new_context() 是干净的、无 Cookie 无缓存的环境;Selenium 的 Chrome() 实例如果不加 --incognito 或清理 profile,会残留上次登录态,导致测试非预期通过或失败。
实操建议:
立即学习“Python免费学习笔记(深入)”;
- Playwright 调试时加
headless=False和slow_mo=500,看操作是否真按预期走 - Selenium 本地跑前务必加
options.add_argument("--incognito"),CI 上反而要用--headless=new(新版 Chrome 推荐) - 跨页面跳转后,Selenium 的
driver.current_url可能滞后,Playwright 的page.url更准,但也要注意 await page.goto() 后是否加了wait_until="networkidle"控制加载完成时机
真实项目里最麻烦的从来不是“怎么点按钮”,而是“怎么确认按钮真的可点了、点了之后状态真的变了、变完之后下一页真的加载完了”——这些细节在 Playwright 里被封装得更稳,在 Selenium 里得靠经验堆判断逻辑。
The explanation of why Playwright is better for new projects should be required reading for anyone starting automation today.
在爬虫项目里用Playwright,比Selenium稳定太多了。那些反爬策略对Playwright效果差一些,可能是因为它更像真实浏览器。
The tip about using page.locator().click() instead of element_handle.click() is important. The former retries, the latter doesn't.
以前写自动化测试最头疼的就是等待策略,显式隐式混用导致各种奇怪超时。Playwright的auto-wait彻底解决了这个问题。
The sandbox isolation in Playwright is great for security-sensitive testing. Selenium's default Chrome instance can leak data between tests.
我们团队用Selenium写了一批登录测试,经常因为验证码识别失败。现在用Playwright加视觉对比,稳定多了。
The warning about ElementNotInteractableError being replaced by auto-wait in Playwright - that's the kind of DX improvement that matters.
文中说Selenium的find_element相对查找很多人误以为是全局,我承认我就是其中之一。debug了好久才发现要在正确的作用域调用。
The performance comparison is spot on. Playwright's architecture with separate browser contexts is just better designed for modern testing.
准备在公司内部推广Playwright,这篇文章可以作为入门培训材料。把核心概念和常见坑都讲清楚了。
The advice about not mixing implicit and explicit waits in Selenium is critical. That combination causes the weirdest timing bugs.