You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从Selenium迁移至Playwright:能否用多定位器定位同一元素?

在Playwright中实现多定位器组合(兼容Selenium原有写法)

当然可以实现,Playwright完全支持CSS选择器的多条件组合,和你在Selenium里用的逗号分隔写法逻辑完全一致——只要元素匹配任意一个选择器,就能被定位到。

你可以直接将原有Selenium中的CSS选择器组合迁移到Playwright中使用,以下是Python示例代码:

# 直接复用原有定位器字符串
loading_locator = page.locator("""div.spinner img[src*=load], .table-spinner-background>div, [alt=Loading], [class*='spinner-border'], [data-ui-handle='circle-spinner'], div.sps-circle, .c-spinner""")

使用这个定位器可以完成各类操作,比如等待加载元素消失:

# 等待加载状态结束(元素隐藏)
loading_locator.wait_for(state="hidden")

或者检查元素是否可见:

if loading_locator.is_visible():
    # 执行对应业务逻辑
    pass

Playwright的locator方法原生支持CSS选择器的多条件语法,和Selenium中By.CSS_SELECTOR的行为完全对齐,不需要额外转换语法。

内容的提问来源于stack exchange,提问作者STK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 20:42:37