A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://playwright.dev/python/docs/pages below:

Pages | Playwright Python

Pages Pages

Each BrowserContext can have multiple pages. A Page refers to a single tab or a popup window within a browser context. It should be used to navigate to URLs and interact with the page content.

page = context.new_page()


page.goto('http://example.com')

page.locator('#search').fill('query')


page.locator('#submit').click()

print(page.url)
page = await context.new_page()


await page.goto('http://example.com')

await page.locator('#search').fill('query')


await page.locator('#submit').click()

print(page.url)
Multiple pages

Each browser context can host multiple pages (tabs).


page_one = context.new_page()
page_two = context.new_page()


all_pages = context.pages

page_one = await context.new_page()
page_two = await context.new_page()


all_pages = context.pages
Handling new pages

The page event on browser contexts can be used to get new pages that are created in the context. This can be used to handle new pages opened by target="_blank" links.


with context.expect_page() as new_page_info:
page.get_by_text("open new tab").click()
new_page = new_page_info.value


new_page.get_by_role("button").click()
print(new_page.title())

async with context.expect_page() as new_page_info:
await page.get_by_text("open new tab").click()
new_page = await new_page_info.value


await new_page.get_by_role("button").click()
print(await new_page.title())

If the action that triggers the new page is unknown, the following pattern can be used.


def handle_page(page):
page.wait_for_load_state()
print(page.title())

context.on("page", handle_page)

async def handle_page(page):
await page.wait_for_load_state()
print(await page.title())

context.on("page", handle_page)
Handling popups

If the page opens a pop-up (e.g. pages opened by target="_blank" links), you can get a reference to it by listening to the popup event on the page.

This event is emitted in addition to the browserContext.on('page') event, but only for popups relevant to this page.


with page.expect_popup() as popup_info:
page.get_by_text("open the popup").click()
popup = popup_info.value


popup.get_by_role("button").click()
print(popup.title())

async with page.expect_popup() as popup_info:
await page.get_by_text("open the popup").click()
popup = await popup_info.value


await popup.get_by_role("button").click()
print(await popup.title())

If the action that triggers the popup is unknown, the following pattern can be used.


def handle_popup(popup):
popup.wait_for_load_state()
print(popup.title())

page.on("popup", handle_popup)

async def handle_popup(popup):
await popup.wait_for_load_state()
print(await popup.title())

page.on("popup", handle_popup)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4