python+playwright 学习-90 and_ 和 or_ 定位

时间:2025-04-07 10:52:41

前言

playwright 从v1.34 版本以后支持and_ 和 or_ 定位

XPath 中的and和or

xpath 语法中我们常用的有text()、contains() 、ends_with()、starts_with()

//*[text()="文本"]
//*[contains(@id, "xx")]
//*[ends-with(@name,'xx')]
//*[starts-with(@id,'xx')]
  • 1
  • 2
  • 3
  • 4

还可以支持and、or、not 关键字定位

//*[@type='submit' and @name='xx']
//*[@name='xx' or @name='yy']
//*[@type='submit' and not(contains(@name,'xxx'))]
  • 1
  • 2
  • 3

playwright 中的and_和or_定位

and_定位示例

button = page.get_by_role("button").and_(page.getByTitle("Subscribe"))
  • 1

or_定位示例

new_email = page.get_by_role("button", name="New")
dialog = page.get_by_text("Confirm security settings")
expect(new_email.or_(dialog)).to_be_visible()
if (dialog.is_visible()):
  page.get_by_role("button", name="Dismiss").click()
new_email.click()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6