playwright元素定位方法收集

1.text selecter

# Find by text.

page.locator("text=Sign up").click()

2.Css selecter

# Find by CSS.

page.locator("#nav-bar .contact-us-item").click()

3.css and text selecter

page.locator("article:has-text('Playwright')").click()

page.locator("#nav-bar :text('Contact us')").click()

4.css and another selecter

page.locator(".item-description:has(.item-promo-banner)").click()

5.layout selecter

# Fill an input to the right of "Username".

page.locator("input:right-of(:text(\"Username\"))").fill("value")

# Click a button near the promo card.

page.locator("button:near(.promo-card)").click()

# Click the radio input in the list closest to the "Label 3".

page.locator("[type=radio]:left-of(:text(\"Label 3\"))").first.click()

2.Filtering Locators

page.locator("button", has_text="Sign up").click()

page.locator("article", has=page.locator("button.subscribe"))

3.pick n-th application

For Example:

<section> <button>Buy</button> </section>

<article><div> <button>Buy</button> </div></article>

<div><div> <button>Buy</button> </div></div>

# Click the third "Buy" button

page.locator(":nth-match(:text('Buy'), 3)").click()

# Wait until all three buttons are visible

page.locator(":nth-match(:text('Buy'), 3)").wait_for()

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容