元素定位
定位器是playwright自动等待和重试功能的核心部分。简而言之,定位器表示一种随时在页面上查找元素的方法。
Locators | Playwright Python
如下这些是推荐的
按显式和隐式辅助功能属性进行定位。
使用F12元素拾取工具,可以看到一个元素的name和role。
page.get_by_role() 定位器反映用户和辅助技术如何感知页面,例如某些元素是按钮还是复选框。按角色查找时,通常还应传递辅助名称,以便定位器精确定位确切的元素。
角色定位器包括按钮、复选框、标题、链接、列表、表等
page.get_by_role("checkbox", name="Subscribe").check()
按文本内容定位。
通过元素包含的文本查找元素。使用 page.get_by_text() 时,可以通过子字符串、精确字符串或正则表达式进行匹配。
expect(page.get_by_text("Welcome, John")).to_be_visible()
这种方式与selenium的根据文本定位一样
根据label标签的文本进行查找
page.get_by_label("Password").fill("secret")
<input type="email" placeholder="name@example.com" />
page.get_by_placeholder("name@example.com").fill("playwright@microsoft.com")
通过其文本替代来定位元素,通常是图像。
<img alt="playwright logo" src="/img/playwright-logo.svg" width="100" />
page.get_by_alt_text("playwright logo").click()
按元素的 title 属性定位元素。
<span title='Issues count'>25 issues</span>
expect(page.get_by_title("Issues count")).to_have_text("25 issues")
<button data-testid="directions">Itinéraire</button>
page.get_by_test_id("directions").click()
xpath/css定位
playwright可以使用xpath或者css定位,如果省略了前缀,playwright会自动检测。
但是不建议使用xpath和css,因为页面变化会导致定位方式变化,导致测试用例执行不稳定。
建议使用role或者test id进行定位。
page.locator("css=button").click()
page.locator("xpath=//button").click()
page.locator("button").click()
page.locator("//button").click()
Locate in Shadow DOM
playwright的定位器默认可以定位到shadow DOM的元素,
除非:
1.使用xpath定位无法穿透 shadow roots
2、shadow roots是closed mode模式的
<x-details role=button aria-expanded=true aria-controls=inner-details>
<div>Title</div>
#shadow-root
<div id=inner-details>Details</div>
</x-details>
定位方法
page.get_by_text("Details").click()
过滤定位
定位器可以使用 locator.filter() 方法按文本进行过滤。它将搜索元素内某处的特定字符串,可能在后代元素中,不区分大小写。您还可以传递正则表达式。
Filter by text
page.get_by_role("listitem").filter(has_text="Product 2").get_by_role(
"button", name="Add to cart"
).click()
Filter by not having text
# 5 in-stock items
expect(page.get_by_role("listitem").filter(has_not_text="Out of stock")).to_have_count(5)
Filter by child/descendant
page.get_by_role("listitem").filter(
has=page.get_by_role("heading", name="Product 2")
).get_by_role("button", name="Add to cart").click()
Filter by not having child/descendant
expect(
page.get_by_role("listitem").filter(
has_not=page.get_by_role("heading", name="Product 2")
)
).to_have_count(1)
缩小定位
链接创建定位器的方法(如 page.get_by_text() 或 locator.get_by_role()),以将搜索范围缩小到页面的特定部分。
Matching inside a locator
product = page.get_by_role("listitem").filter(has_text="Product 2")
product.get_by_role("button", name="Add to cart").click()
同时匹配两个条件
button = page.get_by_role("button").and_(page.getByTitle("Subscribe"))
匹配两个中的一个
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()
只匹配可见元素
page.locator("button").locator("visible=true").click()
定位一组元素
计数
expect(page.get_by_role("listitem")).to_have_count(3)
断言一组元素的文本
expect(page.get_by_role("listitem")).to_have_text(["apple", "banana", "orange"])
在列表中获取特定项
使用文本、测试id、下标进行筛选
page.get_by_text("orange").click()
page.get_by_role("listitem").filter(has_text="orange").click()
page.get_by_test_id("orange").click()
banana = page.get_by_role("listitem").nth(1)
连续过滤
row_locator = page.get_by_role("listitem")
row_locator.filter(has_text="Mary").filter(
has=page.get_by_role("button", name="Say goodbye")
).screenshot(path="screenshot.png")
遍历一组元素
for row in page.get_by_role("listitem").all():
print(row.text_content())
rows = page.get_by_role("listitem")
count = rows.count()
for i in range(count):
print(rows.nth(i).text_content())
evaluate in the pge
rows = page.get_by_role("listitem")
texts = rows.evaluate_all("list => list.map(element => element.textContent)")
这段代码的作用是将`rows`中的每个元素的文本内容提取出来,并返回一个包含提取出的文本的新列表。
代码中使用了`evaluate_all()`函数,它接受一个函数作为参数,并将该函数应用到`rows`列表中的每个元素上。传递的函数采用了箭头函数的形式,接受一个`list`参数,表示`rows`列表,然后使用`map()`方法遍历列表中的每个元素。
在箭头函数中,又传递了一个函数作为`map()`方法的参数,该函数接受一个`element`参数,表示列表中的每个元素。然后,通过使用`element.textContent`获取每个元素的文本内容。
最后,`list.map(element => element.textContent)`返回一个新的列表,该列表包含了提取出的文本内容。
例如,假设`rows`列表包含三个元素,分别是文本内容为"苹果"、"橙子"和"香蕉"的元素。执行该代码后,将返回一个新的列表:`["苹果", "橙子", "香蕉"]`。
不常用的定位器
Other locators | Playwright Python