Best Python code snippet using playwright-python
test_locators.py
Source:test_locators.py  
...417    button = page.locator("body").frame_locator("iframe").locator("button")418    button.wait_for()419    assert button.inner_text() == "Hello iframe"420    button.click()421def route_ambiguous(page: Page) -> None:422    page.route(423        "**/empty.html",424        lambda route: route.fulfill(425            body="""426        <iframe src="iframe-1.html"></iframe>427        <iframe src="iframe-2.html"></iframe>428        <iframe src="iframe-3.html"></iframe>429    """,430            content_type="text/html",431        ),432    )433    page.route(434        "**/iframe-*",435        lambda route: route.fulfill(436            body=f"<html><button>Hello from {urlparse(route.request.url).path[1:]}</button></html>",437            content_type="text/html",438        ),439    )440def test_locator_frame_locator_should_throw_on_ambiguity(441    page: Page, server: Server442) -> None:443    route_ambiguous(page)444    page.goto(server.EMPTY_PAGE)445    button = page.locator("body").frame_locator("iframe").locator("button")446    with pytest.raises(447        Error,448        match='.*strict mode violation: "body >> iframe" resolved to 3 elements.*',449    ):450        button.wait_for()451def test_locator_frame_locator_should_not_throw_on_first_last_nth(452    page: Page, server: Server453) -> None:454    route_ambiguous(page)455    page.goto(server.EMPTY_PAGE)456    button1 = page.locator("body").frame_locator("iframe").first.locator("button")457    assert button1.text_content() == "Hello from iframe-1.html"458    button2 = page.locator("body").frame_locator("iframe").nth(1).locator("button")459    assert button2.text_content() == "Hello from iframe-2.html"460    button3 = page.locator("body").frame_locator("iframe").last.locator("button")461    assert button3.text_content() == "Hello from iframe-3.html"462def test_drag_to(page: Page, server: Server) -> None:463    page.goto(server.PREFIX + "/drag-n-drop.html")464    page.locator("#source").drag_to(page.locator("#target"))465    assert (466        page.eval_on_selector(467            "#target", "target => target.contains(document.querySelector('#source'))"468        )...LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!
