2021-01-01 15:17:27 -08:00
---
2021-11-09 07:42:04 -08:00
id: pages
title: "Pages"
2021-01-01 15:17:27 -08:00
---
2020-12-30 18:04:51 -08:00
2021-01-01 15:17:27 -08:00
<!-- TOC -->
2020-12-30 18:04:51 -08:00
2021-11-09 07:42:04 -08:00
## Pages
2020-12-30 18:04:51 -08:00
2021-11-09 07:42:04 -08:00
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.
2020-12-30 18:04:51 -08:00
```js
2021-11-09 07:42:04 -08:00
// Create a page.
const page = await context.newPage();
// 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.
console.log(page.url());
2020-12-30 18:04:51 -08:00
```
2021-03-01 09:18:44 -08:00
```java
2021-11-09 07:42:04 -08:00
// Create a page.
Page page = context.newPage();
// Navigate explicitly, similar to entering a URL in the browser.
page.navigate("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.
System.out.println(page.url());
2021-03-01 09:18:44 -08:00
```
2021-01-14 15:01:39 -08:00
```python async
2021-11-09 07:42:04 -08:00
page = await context.new_page()
2021-01-14 15:01:39 -08:00
2021-11-09 07:42:04 -08:00
# 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')
2021-01-14 15:01:39 -08:00
2021-11-09 07:42:04 -08:00
# Navigate implicitly by clicking a link.
await page.locator('#submit ').click()
# Expect a new url.
print(page.url)
2021-01-14 15:01:39 -08:00
```
```python sync
2021-11-09 07:42:04 -08:00
page = context.new_page()
2021-01-14 15:01:39 -08:00
2021-11-09 07:42:04 -08:00
# Navigate explicitly, similar to entering a URL in the browser.
page.goto('http://example.com')
# Fill an input.
page.locator('#search ').fill('query')
2021-01-14 15:01:39 -08:00
2021-11-09 07:42:04 -08:00
# Navigate implicitly by clicking a link.
page.locator('#submit ').click()
# Expect a new url.
print(page.url)
2021-01-14 15:01:39 -08:00
```
2020-12-30 18:04:51 -08:00
2021-05-15 10:56:10 -07:00
```csharp
2021-11-09 07:42:04 -08:00
// Create a page.
var page = await context.NewPageAsync();
// Navigate explicitly, similar to entering a URL in the browser.
await page.GotoAsync("http://example.com");
// Fill an input.
await page.Locator("#search ").FillAsync("query");
// Navigate implicitly by clicking a link.
await page.Locator("#submit ").ClickAsync();
// Expect a new url.
Console.WriteLine(page.Url);
2021-05-15 10:56:10 -07:00
```
2020-12-30 18:04:51 -08:00
## Multiple pages
Each browser context can host multiple pages (tabs).
2021-01-14 15:01:39 -08:00
* Each page behaves like a focused, active page. Bringing the page to front is not required.
* Pages inside a context respect context-level emulation, like viewport sizes, custom network routes or browser
locale.
2020-12-30 18:04:51 -08:00
```js
// Create two pages
const pageOne = await context.newPage();
const pageTwo = await context.newPage();
2021-09-17 10:36:51 +02:00
// Get pages of a browser context
2020-12-30 18:04:51 -08:00
const allPages = context.pages();
```
2021-03-01 09:18:44 -08:00
```java
// Create two pages
Page pageOne = context.newPage();
Page pageTwo = context.newPage();
2021-09-17 10:36:51 +02:00
// Get pages of a browser context
2021-03-01 09:18:44 -08:00
List< Page > allPages = context.pages();
```
2021-01-14 15:01:39 -08:00
```python async
# create two pages
page_one = await context.new_page()
page_two = await context.new_page()
2021-09-17 10:36:51 +02:00
# get pages of a browser context
2022-01-07 20:00:54 -05:00
all_pages = context.pages
2021-01-14 15:01:39 -08:00
```
```python sync
# create two pages
page_one = context.new_page()
page_two = context.new_page()
2020-12-30 18:04:51 -08:00
2021-09-17 10:36:51 +02:00
# get pages of a browser context
2022-01-07 20:00:54 -05:00
all_pages = context.pages
2021-01-14 15:01:39 -08:00
```
2021-05-15 10:56:10 -07:00
```csharp
// Create two pages
var pageOne = await context.NewPageAsync();
var pageTwo = await context.NewPageAsync();
2021-09-17 10:36:51 +02:00
// Get pages of a browser context
2021-05-15 10:56:10 -07:00
var allPages = context.Pages;
```
2020-12-30 18:04:51 -08:00
## Handling new pages
2021-01-14 15:01:39 -08:00
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.
2020-12-30 18:04:51 -08:00
```js
2022-11-30 12:36:35 -08:00
// Start waiting for new page before clicking. Note no await.
const pagePromise = context.waitForEvent('page');
await page.getByText('open new tab').click();
const newPage = await pagePromise;
2020-12-30 18:04:51 -08:00
await newPage.waitForLoadState();
console.log(await newPage.title());
```
2021-03-01 09:18:44 -08:00
```java
// Get page after a specific action (e.g. clicking a link)
Page newPage = context.waitForPage(() -> {
2022-11-30 12:36:35 -08:00
page.getByText("open new tab").click(); // Opens a new tab
2021-03-01 09:18:44 -08:00
});
newPage.waitForLoadState();
System.out.println(newPage.title());
```
2021-01-14 15:01:39 -08:00
```python async
# Get page after a specific action (e.g. clicking a link)
async with context.expect_page() as new_page_info:
2022-11-30 12:36:35 -08:00
await page.get_by_text("open new tab").click() # Opens a new tab
2021-01-14 15:01:39 -08:00
new_page = await new_page_info.value
await new_page.wait_for_load_state()
print(await new_page.title())
```
```python sync
# Get page after a specific action (e.g. clicking a link)
with context.expect_page() as new_page_info:
2022-11-30 12:36:35 -08:00
page.get_by_text("open new tab").click() # Opens a new tab
2021-01-14 15:01:39 -08:00
new_page = new_page_info.value
new_page.wait_for_load_state()
print(new_page.title())
```
2021-05-15 10:56:10 -07:00
```csharp
// Get page after a specific action (e.g. clicking a link)
2021-05-26 15:11:31 -07:00
var newPage = await context.RunAndWaitForPageAsync(async () =>
2021-05-19 17:19:25 -07:00
{
2022-11-30 12:36:35 -08:00
await page.GetByText("open new tab").ClickAsync();
2021-05-19 17:19:25 -07:00
});
await newPage.WaitForLoadStateAsync();
2021-05-15 10:56:10 -07:00
Console.WriteLine(await newPage.TitleAsync());
```
2020-12-30 18:04:51 -08:00
If the action that triggers the new page is unknown, the following pattern can be used.
```js
// Get all new pages (including popups) in the context
context.on('page', async page => {
await page.waitForLoadState();
2021-01-14 15:01:39 -08:00
console.log(await page.title());
2023-06-27 11:53:53 +02:00
});
2020-12-30 18:04:51 -08:00
```
2021-03-01 09:18:44 -08:00
```java
// Get all new pages (including popups) in the context
context.onPage(page -> {
page.waitForLoadState();
System.out.println(page.title());
});
```
2021-01-14 15:01:39 -08:00
```python async
# 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)
```
2020-12-30 18:04:51 -08:00
2021-01-14 15:01:39 -08:00
```python sync
# 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)
```
2021-05-15 10:56:10 -07:00
```csharp
// Get all new pages (including popups) in the context
context.Page += async (_, page) => {
await page.WaitForLoadStateAsync();
Console.WriteLine(await page.TitleAsync());
};
```
2020-12-30 18:04:51 -08:00
## Handling popups
2021-09-02 17:21:33 +02:00
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.
2020-12-30 18:04:51 -08:00
2021-01-14 15:01:39 -08:00
This event is emitted in addition to the `browserContext.on('page')` event, but only for popups relevant to this page.
2020-12-30 18:04:51 -08:00
```js
2022-11-30 12:36:35 -08:00
// Start waiting for popup before clicking. Note no await.
const popupPromise = page.waitForEvent('popup');
await page.getByText('open the popup').click();
const popup = await popupPromise;
// Wait for the popup to load.
2020-12-30 18:04:51 -08:00
await popup.waitForLoadState();
2021-01-14 15:01:39 -08:00
console.log(await popup.title());
```
2021-03-01 09:18:44 -08:00
```java
// Get popup after a specific action (e.g., click)
Page popup = page.waitForPopup(() -> {
2022-11-30 12:36:35 -08:00
page.getByText("open the popup").click();
2021-03-01 09:18:44 -08:00
});
popup.waitForLoadState();
System.out.println(popup.title());
```
2021-01-14 15:01:39 -08:00
```python async
# Get popup after a specific action (e.g., click)
async with page.expect_popup() as popup_info:
2022-11-30 12:36:35 -08:00
await page.get_by_text("open the popup").click()
2021-01-14 15:01:39 -08:00
popup = await popup_info.value
await popup.wait_for_load_state()
print(await popup.title())
```
```python sync
# Get popup after a specific action (e.g., click)
with page.expect_popup() as popup_info:
2022-11-30 12:36:35 -08:00
page.get_by_text("open the popup").click()
2021-01-14 15:01:39 -08:00
popup = popup_info.value
popup.wait_for_load_state()
print(popup.title())
2020-12-30 18:04:51 -08:00
```
2021-05-15 10:56:10 -07:00
```csharp
// Get popup after a specific action (e.g., click)
2022-11-30 12:36:35 -08:00
var popup = await page.RunAndWaitForPopupAsync(async () =>
2021-05-19 17:19:25 -07:00
{
2022-11-30 12:36:35 -08:00
await page.GetByText("open the popup").ClickAsync();
2021-05-19 17:19:25 -07:00
});
2022-11-30 12:36:35 -08:00
await popup.WaitForLoadStateAsync();
Console.WriteLine(await popup.TitleAsync());
2021-05-15 10:56:10 -07:00
```
2020-12-30 18:04:51 -08:00
If the action that triggers the popup is unknown, the following pattern can be used.
```js
// Get all popups when they open
page.on('popup', async popup => {
await popup.waitForLoadState();
2022-11-30 12:36:35 -08:00
console.log(await popup.title());
2023-06-27 11:53:53 +02:00
});
2020-12-30 18:04:51 -08:00
```
2021-03-01 09:18:44 -08:00
```java
// Get all popups when they open
page.onPopup(popup -> {
popup.waitForLoadState();
System.out.println(popup.title());
});
```
2021-01-14 15:01:39 -08:00
```python async
# Get all popups when they open
async def handle_popup(popup):
await popup.wait_for_load_state()
print(await popup.title())
2020-12-30 18:04:51 -08:00
2021-01-14 15:01:39 -08:00
page.on("popup", handle_popup)
```
```python sync
# Get all popups when they open
def handle_popup(popup):
popup.wait_for_load_state()
print(popup.title())
page.on("popup", handle_popup)
```
2021-05-15 10:56:10 -07:00
```csharp
// Get all popups when they open
page.Popup += async (_, popup) => {
await popup.WaitForLoadStateAsync();
Console.WriteLine(await page.TitleAsync());
};
```