ページ
ページ
各BrowserContextは複数のページを持つことができます。ページは、ブラウザコンテキスト内の単一のタブまたはポップアップウィンドウを指します。URL に移動し、ページコンテンツを操作するために使用する必要があります。
- 同期
- 非同期
page = context.new_page()
# Navigate explicitly, similar to entering a URL in the browser.
page.goto('http://example.com')
# Fill an input.
page.locator('#search').fill('query')
# Navigate implicitly by clicking a link.
page.locator('#submit').click()
# Expect a new url.
print(page.url)
page = await context.new_page()
# Navigate explicitly, similar to entering a URL in the browser.
await page.goto('http://example.com')
# Fill an input.
await page.locator('#search').fill('query')
# Navigate implicitly by clicking a link.
await page.locator('#submit').click()
# Expect a new url.
print(page.url)
複数ページ
各ブラウザコンテキストは、複数のページ (タブ) をホストできます。
- 各ページは、フォーカスされたアクティブなページのように動作します。ページを前面に表示する必要はありません。
- コンテキスト内のページは、ビューポートサイズ、カスタムネットワークルート、またはブラウザロケールなどのコンテキストレベルのエミュレーションを尊重します。
- 同期
- 非同期
# create two pages
page_one = context.new_page()
page_two = context.new_page()
# get pages of a browser context
all_pages = context.pages
# create two pages
page_one = await context.new_page()
page_two = await context.new_page()
# get pages of a browser context
all_pages = context.pages
新しいページの処理
ブラウザコンテキストの page
イベントを使用して、コンテキストで作成された新しいページを取得できます。これは、target="_blank"
リンクによって開かれた新しいページを処理するために使用できます。
- 同期
- 非同期
# Get page after a specific action (e.g. clicking a link)
with context.expect_page() as new_page_info:
page.get_by_text("open new tab").click() # Opens a new tab
new_page = new_page_info.value
# Interact with the new page normally
new_page.get_by_role("button").click()
print(new_page.title())
# Get page after a specific action (e.g. clicking a link)
async with context.expect_page() as new_page_info:
await page.get_by_text("open new tab").click() # Opens a new tab
new_page = await new_page_info.value
# Interact with the new page normally
await new_page.get_by_role("button").click()
print(await new_page.title())
新しいページをトリガーするアクションが不明な場合は、次のパターンを使用できます。
- 同期
- 非同期
# Get all new pages (including popups) in the context
def handle_page(page):
page.wait_for_load_state()
print(page.title())
context.on("page", handle_page)
# Get all new pages (including popups) in the context
async def handle_page(page):
await page.wait_for_load_state()
print(await page.title())
context.on("page", handle_page)
ポップアップの処理
ページがポップアップを開く場合 (例: target="_blank"
リンクによって開かれたページ)、ページの popup
イベントをリッスンすることで、ポップアップへの参照を取得できます。
このイベントは、browserContext.on('page')
イベントに加えて発行されますが、このページに関連するポップアップのみに発行されます。
- 同期
- 非同期
# Get popup after a specific action (e.g., click)
with page.expect_popup() as popup_info:
page.get_by_text("open the popup").click()
popup = popup_info.value
# Interact with the popup normally
popup.get_by_role("button").click()
print(popup.title())
# Get popup after a specific action (e.g., click)
async with page.expect_popup() as popup_info:
await page.get_by_text("open the popup").click()
popup = await popup_info.value
# Interact with the popup normally
await popup.get_by_role("button").click()
print(await popup.title())
ポップアップをトリガーするアクションが不明な場合は、次のパターンを使用できます。
- 同期
- 非同期
# Get all popups when they open
def handle_popup(popup):
popup.wait_for_load_state()
print(popup.title())
page.on("popup", handle_popup)
# Get all popups when they open
async def handle_popup(popup):
await popup.wait_for_load_state()
print(await popup.title())
page.on("popup", handle_popup)