2021-01-01 15:17:27 -08:00
---
id: core-concepts
title: "Core concepts"
---
2020-12-30 18:04:51 -08:00
Playwright provides a set of APIs to automate Chromium, Firefox and WebKit
2021-01-20 16:06:26 -08:00
browsers. By using the Playwright API, you can write scripts to create
2020-12-30 18:04:51 -08:00
new browser pages, navigate to URLs and then interact with elements on a page.
Along with a test runner Playwright can be used to automate user interactions to
validate and test web applications. The Playwright API enables this through
the following primitives.
2021-01-01 15:17:27 -08:00
<!-- TOC -->
2020-12-30 18:04:51 -08:00
< br / >
## Browser
A [Browser] refers to an instance of Chromium, Firefox
or WebKit. Playwright scripts generally start with launching a browser instance
and end with closing the browser. Browser instances can be launched in headless
2021-04-01 20:13:50 +02:00
(without a GUI) or headed mode.
2020-12-30 18:04:51 -08:00
```js
const { chromium } = require('playwright'); // Or 'firefox' or 'webkit'.
const browser = await chromium.launch({ headless: false });
await browser.close();
```
2021-03-01 09:18:44 -08:00
```java
import com.microsoft.playwright.*;
public class Example {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
BrowserType chromium = playwright.chromium();
2021-03-05 13:50:34 -08:00
Browser browser = chromium.launch(new BrowserType.LaunchOptions().setHeadless(false));
2021-03-01 09:18:44 -08:00
browser.close();
}
}
}
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
import asyncio
2021-01-11 17:04:24 -08:00
from playwright.async_api import async_playwright
2021-01-08 17:39:33 +01:00
async def main():
async with async_playwright() as p:
browser = await p.chromium.launch(headless=False)
await browser.close()
2021-01-15 09:12:47 -08:00
asyncio.run(main())
2021-01-08 17:39:33 +01:00
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-11 17:04:24 -08:00
from playwright.sync_api import sync_playwright
2021-01-08 17:39:33 +01:00
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
browser.close()
```
2021-05-20 04:53:12 +02:00
```csharp
using Microsoft.Playwright;
using System.Threading.Tasks;
2021-05-22 07:55:53 -07:00
class Program
2021-05-20 04:53:12 +02:00
{
public static async Task Main()
{
using var playwright = await Playwright.CreateAsync();
2021-05-20 08:20:21 -07:00
await using var firefox = playwright.Firefox.LaunchAsync(new BrowserTypeLaunchOptions
{
Headless = false
});
2021-05-20 04:53:12 +02:00
}
}
```
2020-12-30 18:04:51 -08:00
Launching a browser instance can be expensive, and Playwright is designed to
maximize what a single instance can do through multiple browser contexts.
2021-01-17 21:09:40 -08:00
### API reference
2020-12-30 18:04:51 -08:00
- [Browser]
< br / >
## Browser contexts
A [BrowserContext] is an isolated incognito-alike
session within a browser instance. Browser contexts are fast and cheap to create.
2021-05-19 21:14:17 +00:00
We recommend running each test scenario in its own new Browser context, so that
the browser state is isolated between the tests.
2020-12-30 18:04:51 -08:00
```js
const browser = await chromium.launch();
const context = await browser.newContext();
```
2021-03-01 09:18:44 -08:00
```java
Browser browser = chromium.launch();
BrowserContext context = browser.newContext();
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
browser = await playwright.chromium.launch()
context = await browser.new_context()
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
browser = playwright.chromium.launch()
context = browser.new_context()
```
2021-05-20 04:53:12 +02:00
```csharp
2021-06-03 08:08:05 -07:00
await using var browser = playwright.Chromium.LaunchAsync();
2021-05-20 04:53:12 +02:00
var context = await browser.NewContextAsync();
```
2020-12-30 18:04:51 -08:00
Browser contexts can also be used to emulate multi-page scenarios involving
mobile devices, permissions, locale and color scheme.
```js
const { devices } = require('playwright');
const iPhone = devices['iPhone 11 Pro'];
const context = await browser.newContext({
...iPhone,
permissions: ['geolocation'],
geolocation: { latitude: 52.52, longitude: 13.39},
colorScheme: 'dark',
locale: 'de-DE'
});
```
2021-03-01 09:18:44 -08:00
```java
// FIXME
import com.microsoft.playwright.*;
public class Example {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
BrowserType devices = playwright.devices();
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
2021-03-05 13:50:34 -08:00
.setUserAgent("Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Mobile/15E148 Safari/604.1")
.setViewportSize(375, 812)
.setDeviceScaleFactor(3)
.setIsMobile(true)
.setHasTouch(true)
.setPermissions(Arrays.asList("geolocation"))
.setGeolocation(52.52, 13.39)
.setColorScheme(ColorScheme.DARK)
.setLocale("de-DE"));
2021-03-01 09:18:44 -08:00
}
}
}
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
import asyncio
2021-01-11 17:04:24 -08:00
from playwright.async_api import async_playwright
2021-01-08 17:39:33 +01:00
async def main():
async with async_playwright() as p:
iphone_11 = p.devices['iPhone 11 Pro']
browser = await p.chromium.launch()
context = await browser.new_context(
**iphone_11,
locale='de-DE',
geolocation={ 'longitude': 12.492507, 'latitude': 41.889938 },
permissions=['geolocation'],
color_scheme='dark',
)
2021-02-01 11:47:03 -08:00
page = await browser.new_page()
2021-01-08 17:39:33 +01:00
await browser.close()
2021-01-15 09:12:47 -08:00
asyncio.run(main())
2021-01-08 17:39:33 +01:00
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-11 17:04:24 -08:00
from playwright.sync_api import sync_playwright
2021-01-08 17:39:33 +01:00
with sync_playwright() as p:
iphone_11 = p.devices['iPhone 11 Pro']
browser = p.webkit.launch(headless=False)
context = browser.new_context(
**iphone_11,
locale='de-DE',
geolocation={ 'longitude': 12.492507, 'latitude': 41.889938 },
permissions=['geolocation']
)
browser.close()
```
2021-05-20 04:53:12 +02:00
```csharp
using Microsoft.Playwright;
using System.Threading.Tasks;
class PlaywrightExample
{
2021-05-20 08:20:21 -07:00
public static async Task Main()
{
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Webkit.LaunchAsync();
var options = new BrowserContextNewOptions(Playwright.Devices["iPhone 11 Pro"])
{
Geolocation = new Geolocation() { Longitude = 12.492507f, Latitude = 41.889938f },
Permissions = new[] { "geolocation" },
Locale = "de-DE"
};
2021-05-20 04:53:12 +02:00
2021-05-20 08:20:21 -07:00
await using var context = await browser.NewContextAsync(options);
// do work
}
2021-05-20 04:53:12 +02:00
}
```
2021-01-17 21:09:40 -08:00
### API reference
2020-12-30 18:04:51 -08:00
- [BrowserContext]
- [`method: Browser.newContext` ]
< br / >
## Pages and frames
A Browser context 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.
```js
// 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.fill('#search ', 'query');
// Navigate implicitly by clicking a link.
await page.click('#submit ');
// Expect a new url.
console.log(page.url());
// Page can navigate from the script - this will be picked up by Playwright.
window.location.href = 'https://example.com';
```
2021-03-01 09:18:44 -08:00
```java
// 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.fill("#search ", "query");
// Navigate implicitly by clicking a link.
page.click("#submit ");
// Expect a new url.
System.out.println(page.url());
// Page can navigate from the script - this will be picked up by Playwright.
// window.location.href = "https://example.com";
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
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.fill('#search ', 'query')
# Navigate implicitly by clicking a link.
await page.click('#submit ')
# Expect a new url.
print(page.url)
# Page can navigate from the script - this will be picked up by Playwright.
# window.location.href = 'https://example.com'
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
page = context.new_page()
# Navigate explicitly, similar to entering a URL in the browser.
page.goto('http://example.com')
# Fill an input.
page.fill('#search ', 'query')
# Navigate implicitly by clicking a link.
page.click('#submit ')
# Expect a new url.
print(page.url)
# Page can navigate from the script - this will be picked up by Playwright.
# window.location.href = 'https://example.com'
```
2021-05-20 04:53:12 +02:00
```csharp
// 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.FillAsync("#search ", "query");
// Navigate implicitly by clicking a link.
await page.ClickAsync("#submit ");
// Expect a new url.
Console.WriteLine(page.Url);
// Page can navigate from the script - this will be picked up by Playwright.
// window.location.href = "https://example.com";
```
2020-12-30 18:04:51 -08:00
> Read more on [page navigation and loading](./navigations.md).
A page can have one or more [Frame] objects attached to
it. Each page has a main frame and page-level interactions (like `click` ) are
assumed to operate in the main frame.
A page can have additional frames attached with the `iframe` HTML tag. These
frames can be accessed for interactions inside the frame.
```js
// Get frame using the frame's name attribute
const frame = page.frame('frame-login');
// Get frame using frame's URL
const frame = page.frame({ url: /.*domain.*/ });
// Get frame using any other selector
const frameElementHandle = await page.$('.frame-class');
const frame = await frameElementHandle.contentFrame();
// Interact with the frame
await frame.fill('#username -input', 'John');
```
2021-03-01 09:18:44 -08:00
```java
// Get frame using the frame"s name attribute
Frame frame = page.frame("frame-login");
// Get frame using frame"s URL
Frame frame = page.frameByUrl(Pattern.compile(".*domain.*"));
// Get frame using any other selector
ElementHandle frameElementHandle = page.querySelector(".frame-class");
Frame frame = frameElementHandle.contentFrame();
// Interact with the frame
frame.fill("#username -input", "John");
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
# Get frame using the frame's name attribute
frame = page.frame('frame-login')
# Get frame using frame's URL
frame = page.frame(url=r'.*domain.*')
# Get frame using any other selector
frame_element_handle = await page.query_selector('.frame-class')
frame = await frame_element_handle.content_frame()
# Interact with the frame
await frame.fill('#username -input', 'John')
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
# Get frame using the frame's name attribute
frame = page.frame('frame-login')
# Get frame using frame's URL
frame = page.frame(url=r'.*domain.*')
# Get frame using any other selector
frame_element_handle = page.query_selector('.frame-class')
frame = frame_element_handle.content_frame()
# Interact with the frame
frame.fill('#username -input', 'John')
```
2021-05-20 04:53:12 +02:00
```csharp
// Create a page.
var page = await context.NewPageAsync();
// Get frame using the frame's name attribute
var frame = page.Frame("frame-login");
// Get frame using frame's URL
var frame = page.FrameByUrl("*domain.");
// Get frame using any other selector
var frameElementHandle = await page.QuerySelectorAsync(".frame-class");
var frame = await frameElementHandle.ContentFrameAsync();
// Interact with the frame
await frame.FillAsync("#username -input", "John");
```
2021-01-17 21:09:40 -08:00
### API reference
2020-12-30 18:04:51 -08:00
- [Page]
- [Frame]
- [`method: Page.frame` ]
< br / >
## Selectors
Playwright can search for elements using CSS selectors, XPath selectors, HTML attributes like `id` , `data-test-id` and even text content.
You can explicitly specify the selector engine you are using or let Playwright detect it.
All selector engines except for XPath pierce shadow DOM by default. If you want to enforce regular DOM selection, you can use the `*:light` versions of the selectors. You don't typically need to though.
Learn more about selectors and selector engines [here ](./selectors.md ).
Some examples below:
```js
// Using data-test-id= selector engine
await page.click('data-test-id=foo');
```
2021-03-01 09:18:44 -08:00
```java
// Using data-test-id= selector engine
page.click("data-test-id=foo");
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
# Using data-test-id= selector engine
await page.click('data-test-id=foo')
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
# Using data-test-id= selector engine
page.click('data-test-id=foo')
```
2021-05-20 04:53:12 +02:00
```csharp
// Using data-test-id= selector engine
await page.ClickAsync("data-test-id=foo");
```
2020-12-30 18:04:51 -08:00
```js
// CSS and XPath selector engines are automatically detected
await page.click('div');
await page.click('//html/body/div');
```
2021-03-01 09:18:44 -08:00
```java
// CSS and XPath selector engines are automatically detected
page.click("div");
page.click("//html/body/div");
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
# CSS and XPath selector engines are automatically detected
await page.click('div')
await page.click('//html/body/div')
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
# CSS and XPath selector engines are automatically detected
page.click('div')
page.click('//html/body/div')
```
2021-05-20 04:53:12 +02:00
```csharp
// CSS and XPath selector engines are automatically detected
await page.ClickAsync("div");
await page.ClickAsync("//html/body/div");
```
2020-12-30 18:04:51 -08:00
```js
// Find node by text substring
await page.click('text=Hello w');
```
2021-03-01 09:18:44 -08:00
```java
// Find node by text substring
page.click("text=Hello w");
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
# Find node by text substring
await page.click('text=Hello w')
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
# Find node by text substring
page.click('text=Hello w')
```
2021-05-20 04:53:12 +02:00
```csharp
// Find node by text substring
await page.ClickAsync("text=Hello w");
```
2020-12-30 18:04:51 -08:00
```js
// Explicit CSS and XPath notation
await page.click('css=div');
await page.click('xpath=//html/body/div');
```
2021-03-01 09:18:44 -08:00
```java
// Explicit CSS and XPath notation
page.click("css=div");
page.click("xpath=//html/body/div");
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
# Explicit CSS and XPath notation
await page.click('css=div')
await page.click('xpath=//html/body/div')
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
# Explicit CSS and XPath notation
page.click('css=div')
page.click('xpath=//html/body/div')
```
2021-05-20 04:53:12 +02:00
```csharp
// Explicit CSS and XPath notation
await page.ClickAsync("css=div");
await page.ClickAsync("xpath=//html/body/div");
```
2020-12-30 18:04:51 -08:00
```js
// Only search light DOM, outside WebComponent shadow DOM:
await page.click('css:light=div');
```
2021-03-01 09:18:44 -08:00
```java
// Only search light DOM, outside WebComponent shadow DOM:
page.click("css:light=div");
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
# Only search light DOM, outside WebComponent shadow DOM:
await page.click('css:light=div')
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
# Only search light DOM, outside WebComponent shadow DOM:
page.click('css:light=div')
```
2021-05-20 04:53:12 +02:00
```csharp
// Only search light DOM, outside WebComponent shadow DOM:
await page.ClickAsync("css:light=div");
```
2020-12-30 18:04:51 -08:00
Selectors using the same or different engines can be combined using the `>>` separator. For example,
```js
// Click an element with text 'Sign Up' inside of a #free -month-promo.
await page.click('#free -month-promo >> text=Sign Up');
```
2021-03-01 09:18:44 -08:00
```java
// Click an element with text "Sign Up" inside of a #free -month-promo.
page.click("#free -month-promo >> text=Sign Up");
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
# Click an element with text 'Sign Up' inside of a #free-month-promo.
await page.click('#free -month-promo >> text=Sign Up')
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
# Click an element with text 'Sign Up' inside of a #free-month-promo.
page.click('#free -month-promo >> text=Sign Up')
```
2021-05-20 04:53:12 +02:00
```csharp
// Click an element with text "Sign Up" inside of a #free -month-promo.
await page.Click("#free -month-promo >> text=Sign Up");
```
2020-12-30 18:04:51 -08:00
```js
// Capture textContent of a section that contains an element with text 'Selectors'.
const sectionText = await page.$eval('*css=section >> text=Selectors', e => e.textContent);
```
2021-03-01 09:18:44 -08:00
```java
// Capture textContent of a section that contains an element with text "Selectors".
String sectionText = (String) page.evalOnSelector("*css=section >> text=Selectors", "e => e.textContent");
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
# Capture textContent of a section that contains an element with text 'Selectors'.
section_text = await page.eval_on_selector('*css=section >> text=Selectors', 'e => e.textContent')
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
# Capture textContent of a section that contains an element with text 'Selectors'.
section_text = page.eval_on_selector('*css=section >> text=Selectors', 'e => e.textContent')
```
2021-05-20 04:53:12 +02:00
```csharp
// Capture textContent of a section that contains an element with text "Selectors".
var sectionText = await page.EvalOnSelectorAsync< string > ("*css=section >> text=Selectors", "e => e.textContent");
```
2020-12-30 18:04:51 -08:00
< br / >
## Auto-waiting
2021-01-06 11:59:29 -08:00
Actions like [`method: Page.click` ] and [`method: Page.fill` ] auto-wait for the element to be visible
and [actionable ](./actionability.md ). For example, click will:
2020-12-30 18:04:51 -08:00
- wait for an element with the given selector to appear in the DOM
- wait for it to become visible: have non-empty bounding box and no `visibility:hidden`
- wait for it to stop moving: for example, wait until css transition finishes
- scroll the element into view
- wait for it to receive pointer events at the action point: for example, wait until element becomes non-obscured by other elements
- retry if the element is detached during any of the above checks
```js
// Playwright waits for #search element to be in the DOM
await page.fill('#search ', 'query');
```
2021-01-08 17:39:33 +01:00
2021-03-01 09:18:44 -08:00
```java
// Playwright waits for #search element to be in the DOM
page.fill("#search ", "query");
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
# Playwright waits for #search element to be in the DOM
await page.fill('#search ', 'query')
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
# Playwright waits for #search element to be in the DOM
page.fill('#search ', 'query')
```
2021-05-20 04:53:12 +02:00
```csharp
// Playwright waits for #search element to be in the DOM
await page.FillAsync("#search ", "query");
```
2020-12-30 18:04:51 -08:00
```js
// Playwright waits for element to stop animating
// and accept clicks.
await page.click('#search ');
```
2021-03-01 09:18:44 -08:00
```java
// Playwright waits for element to stop animating
// and accept clicks.
page.click("#search ");
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
# Playwright waits for element to stop animating
# and accept clicks.
await page.click('#search ')
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
# Playwright waits for element to stop animating
# and accept clicks.
page.click('#search ')
```
2021-05-20 04:53:12 +02:00
```csharp
// Playwright waits for element to stop animating
// and accept clicks.
await page.ClickAsync("#search ");
```
2020-12-30 18:04:51 -08:00
You can explicitly wait for an element to appear in the DOM or to become visible:
```js
// Wait for #search to appear in the DOM.
await page.waitForSelector('#search ', { state: 'attached' });
// Wait for #promo to become visible, for example with `visibility:visible` .
await page.waitForSelector('#promo ');
```
2021-03-01 09:18:44 -08:00
```java
// Wait for #search to appear in the DOM.
page.waitForSelector("#search ", new Page.WaitForSelectorOptions()
2021-03-05 13:50:34 -08:00
.setState(WaitForSelectorState.ATTACHED));
2021-03-01 09:18:44 -08:00
// Wait for #promo to become visible, for example with "visibility:visible".
page.waitForSelector("#promo ");
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
# Wait for #search to appear in the DOM.
await page.wait_for_selector('#search ', state='attached')
# Wait for #promo to become visible, for example with `visibility:visible`.
await page.wait_for_selector('#promo ')
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
# Wait for #search to appear in the DOM.
page.wait_for_selector('#search ', state='attached')
# Wait for #promo to become visible, for example with `visibility:visible`.
page.wait_for_selector('#promo ')
```
2021-05-20 04:53:12 +02:00
```csharp
// Wait for #search to appear in the DOM.
await page.WaitForSelectorAsync("#search ", WaitForSelectorState.Attached);
// Wait for #promo to become visible, for example with `visibility:visible` .
await page.WaitForSelectorAsync("#promo ");
```
2020-12-30 18:04:51 -08:00
... or to become hidden or detached
```js
// Wait for #details to become hidden, for example with `display:none` .
await page.waitForSelector('#details ', { state: 'hidden' });
// Wait for #promo to be removed from the DOM.
await page.waitForSelector('#promo ', { state: 'detached' });
```
2021-03-01 09:18:44 -08:00
```java
// Wait for #details to become hidden, for example with "display:none".
page.waitForSelector("#details ", new Page.WaitForSelectorOptions()
2021-03-05 13:50:34 -08:00
.setState(WaitForSelectorState.HIDDEN));
2021-03-01 09:18:44 -08:00
// Wait for #promo to be removed from the DOM.
page.waitForSelector("#promo ", new Page.WaitForSelectorOptions()
2021-03-05 13:50:34 -08:00
.setState(WaitForSelectorState.DETACHED));
2021-03-01 09:18:44 -08:00
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
# Wait for #details to become hidden, for example with `display:none`.
await page.wait_for_selector('#details ', state='hidden')
# Wait for #promo to be removed from the DOM.
await page.wait_for_selector('#promo ', state='detached')
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
# Wait for #details to become hidden, for example with `display:none`.
page.wait_for_selector('#details ', state='hidden')
# Wait for #promo to be removed from the DOM.
page.wait_for_selector('#promo ', state='detached')
```
2021-05-20 04:53:12 +02:00
```csharp
// Wait for #details to become hidden, for example with "display:none".
await page.WaitForSelectorAsync("#details ", WaitForSelectorState.Hidden);
// Wait for #promo to be removed from the DOM.
await page.WaitForSelectorAsync("#promo ", WaitForSelectorState.Detached);
```
2021-01-17 21:09:40 -08:00
### API reference
2020-12-30 18:04:51 -08:00
- [`method: Page.click` ]
- [`method: Page.fill` ]
- [`method: Page.waitForSelector` ]
< br / >
2021-01-17 21:09:40 -08:00
## Execution contexts: Playwright and Browser
2020-12-30 18:04:51 -08:00
2021-01-17 21:09:40 -08:00
Playwright scripts run in your Playwright environment. Your page scripts run in the browser page environment. Those environments don't intersect, they are running in different virtual machines in different processes and even potentially on different computers.
2020-12-30 18:04:51 -08:00
The [`method: Page.evaluate` ] API can run a JavaScript function in the context
2021-01-17 21:09:40 -08:00
of the web page and bring results back to the Playwright environment. Browser globals like
2020-12-30 18:04:51 -08:00
`window` and `document` can be used in `evaluate` .
```js
const href = await page.evaluate(() => document.location.href);
```
2021-03-01 09:18:44 -08:00
```java
String href = (String) page.evaluate("document.location.href");
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
href = await page.evaluate('() => document.location.href')
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
href = page.evaluate('() => document.location.href')
```
2021-05-20 04:53:12 +02:00
```csharp
var href = await page.EvaluateAsync< string > ("document.location.href");
```
2020-12-30 18:04:51 -08:00
If the result is a Promise or if the function is asynchronous evaluate will automatically wait until it's resolved:
2021-01-08 17:39:33 +01:00
2020-12-30 18:04:51 -08:00
```js
const status = await page.evaluate(async () => {
const response = await fetch(location.href);
return response.status;
});
```
2021-03-01 09:18:44 -08:00
```java
int status = (int) page.evaluate("async () => {\n" +
" const response = await fetch(location.href);\n" +
" return response.status;\n" +
"}");
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
status = await page.evaluate("""async () => {
response = await fetch(location.href)
return response.status
}""")
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
status = page.evaluate("""async () => {
response = fetch(location.href)
return response.status
}""")
```
2021-05-20 04:53:12 +02:00
```csharp
int status = await page.EvaluateAsync< int > (@"async () => {
const response = await fetch(location.href);
return response.status;
}");
```
2021-01-01 15:17:27 -08:00
## Evaluation Argument
Playwright evaluation methods like [`method: Page.evaluate` ] take a single optional argument. This argument can be a mix of [Serializable] values and [JSHandle] or [ElementHandle] instances. Handles are automatically converted to the value they represent.
```js
// A primitive value.
await page.evaluate(num => num, 42);
// An array.
await page.evaluate(array => array.length, [1, 2, 3]);
// An object.
await page.evaluate(object => object.foo, { foo: 'bar' });
// A single handle.
const button = await page.$('button');
await page.evaluate(button => button.textContent, button);
// Alternative notation using elementHandle.evaluate.
await button.evaluate((button, from) => button.textContent.substring(from), 5);
// Object with multiple handles.
const button1 = await page.$('.button1');
const button2 = await page.$('.button2');
await page.evaluate(
o => o.button1.textContent + o.button2.textContent,
{ button1, button2 });
// Object destructuring works. Note that property names must match
// between the destructured object and the argument.
// Also note the required parenthesis.
await page.evaluate(
({ button1, button2 }) => button1.textContent + button2.textContent,
{ button1, button2 });
// Array works as well. Arbitrary names can be used for destructuring.
// Note the required parenthesis.
await page.evaluate(
([b1, b2]) => b1.textContent + b2.textContent,
[button1, button2]);
// Any non-cyclic mix of serializables and handles works.
await page.evaluate(
x => x.button1.textContent + x.list[0].textContent + String(x.foo),
{ button1, list: [button2], foo: null });
```
2020-12-30 18:04:51 -08:00
2021-03-01 09:18:44 -08:00
```java
// A primitive value.
page.evaluate("num => num", 42);
// An array.
page.evaluate("array => array.length", Arrays.asList(1, 2, 3));
// An object.
Map< String , Object > obj = new HashMap< >();
obj.put("foo", "bar");
page.evaluate("object => object.foo", obj);
// A single handle.
ElementHandle button = page.querySelector("button");
page.evaluate("button => button.textContent", button);
// Alternative notation using elementHandle.evaluate.
button.evaluate("(button, from) => button.textContent.substring(from)", 5);
// Object with multiple handles.
ElementHandle button1 = page.querySelector(".button1");
ElementHandle button2 = page.querySelector(".button2");
Map< String , ElementHandle > arg = new HashMap< >();
arg.put("button1", button1);
arg.put("button2", button2);
page.evaluate("o => o.button1.textContent + o.button2.textContent", arg);
// Object destructuring works. Note that property names must match
// between the destructured object and the argument.
// Also note the required parenthesis.
Map< String , ElementHandle > arg = new HashMap< >();
arg.put("button1", button1);
arg.put("button2", button2);
page.evaluate("({ button1, button2 }) => button1.textContent + button2.textContent", arg);
// Array works as well. Arbitrary names can be used for destructuring.
// Note the required parenthesis.
page.evaluate(
"([b1, b2]) => b1.textContent + b2.textContent",
Arrays.asList(button1, button2));
// Any non-cyclic mix of serializables and handles works.
Map< String , Object > arg = new HashMap< >();
arg.put("button1", button1);
arg.put("list", Arrays.asList(button2));
arg.put("foo", 0);
page.evaluate(
"x => x.button1.textContent + x.list[0].textContent + String(x.foo)",
arg);
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
# A primitive value.
await page.evaluate('num => num', 42)
# An array.
await page.evaluate('array => array.length', [1, 2, 3])
# An object.
await page.evaluate('object => object.foo', { 'foo': 'bar' })
# A single handle.
button = await page.query_selctor('button')
await page.evaluate('button => button.textContent', button)
# Alternative notation using elementHandle.evaluate.
await button.evaluate('(button, from) => button.textContent.substring(from)', 5)
# Object with multiple handles.
button1 = await page.query_selector('.button1')
button2 = await page.query_selector('.button2')
await page.evaluate("""
o => o.button1.textContent + o.button2.textContent""",
{ 'button1': button1, 'button2': button2 })
# Object destructuring works. Note that property names must match
# between the destructured object and the argument.
# Also note the required parenthesis.
await page.evaluate("""
({ button1, button2 }) => button1.textContent + button2.textContent""",
{ 'button1': button1, 'button2': button2 })
# Array works as well. Arbitrary names can be used for destructuring.
# Note the required parenthesis.
await page.evaluate("""
([b1, b2]) => b1.textContent + b2.textContent""",
[button1, button2])
# Any non-cyclic mix of serializables and handles works.
await page.evaluate("""
x => x.button1.textContent + x.list[0].textContent + String(x.foo)""",
{ 'button1': button1, 'list': [button2], 'foo': None })
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
# A primitive value.
page.evaluate('num => num', 42)
# An array.
page.evaluate('array => array.length', [1, 2, 3])
# An object.
page.evaluate('object => object.foo', { 'foo': 'bar' })
# A single handle.
button = page.query_selector('button')
page.evaluate('button => button.textContent', button)
# Alternative notation using elementHandle.evaluate.
button.evaluate('(button, from) => button.textContent.substring(from)', 5)
# Object with multiple handles.
button1 = page.query_selector('.button1')
button2 = page.query_selector('.button2')
page.evaluate("""o => o.button1.textContent + o.button2.textContent""",
{ 'button1': button1, 'button2': button2 })
# Object destructuring works. Note that property names must match
# between the destructured object and the argument.
# Also note the required parenthesis.
page.evaluate("""
({ button1, button2 }) => button1.textContent + button2.textContent""",
{ 'button1': button1, 'button2': button2 })
# Array works as well. Arbitrary names can be used for destructuring.
# Note the required parenthesis.
page.evaluate("""
([b1, b2]) => b1.textContent + b2.textContent""",
[button1, button2])
# Any non-cyclic mix of serializables and handles works.
page.evaluate("""
x => x.button1.textContent + x.list[0].textContent + String(x.foo)""",
{ 'button1': button1, 'list': [button2], 'foo': None })
```
2020-12-30 18:04:51 -08:00
2021-05-20 04:53:12 +02:00
```csharp
// A primitive value.
await page.EvaluateAsync< int > ("num => num", 42);
// An array.
await page.EvaluateAsync< int [ ] > ("array => array.length", new[] { 1, 2, 3 });
// An object.
await page.EvaluateAsync< object > ("object => object.foo", new { foo = "bar" });
// A single handle.
var button = await page.QuerySelectorAsync("button");
await page.EvaluateAsync< IJSHandle > ("button => button.textContent", button);
// Alternative notation using elementHandle.EvaluateAsync.
await button.EvaluateAsync< string > ("(button, from) => button.textContent.substring(from)", 5);
// Object with multiple handles.
var button1 = await page.QuerySelectorAsync(".button1");
var button2 = await page.QuerySelectorAsync(".button2");
await page.EvaluateAsync("o => o.button1.textContent + o.button2.textContent", new { button1, button2 });
// Object destructuring works. Note that property names must match
// between the destructured object and the argument.
// Also note the required parenthesis.
await page.EvaluateAsync("({ button1, button2 }) => button1.textContent + button2.textContent", new { button1, button2 });
// Array works as well. Arbitrary names can be used for destructuring.
// Note the required parenthesis.
await page.EvaluateAsync("([b1, b2]) => b1.textContent + b2.textContent", new[] { button1, button2 });
// Any non-cyclic mix of serializables and handles works.
await page.EvaluateAsync("x => x.button1.textContent + x.list[0].textContent + String(x.foo)", new { button1, list = new[] { button2 }, foo = null as object });
```
2020-12-30 18:04:51 -08:00
Right:
```js
const data = { text: 'some data', value: 1 };
// Pass |data| as a parameter.
const result = await page.evaluate(data => {
window.myApp.use(data);
}, data);
```
2021-03-01 09:18:44 -08:00
```java
Map< String , Object > data = new HashMap< >();
data.put("text", "some data");
data.put("value", 1);
// Pass |data| as a parameter.
Object result = page.evaluate("data => {\n" +
" window.myApp.use(data);\n" +
"}", data);
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
data = { 'text': 'some data', 'value': 1 }
# Pass |data| as a parameter.
result = await page.evaluate("""data => {
window.myApp.use(data)
}""", data)
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
data = { 'text': 'some data', 'value': 1 }
# Pass |data| as a parameter.
result = page.evaluate("""data => {
window.myApp.use(data)
}""", data)
```
2021-05-20 04:53:12 +02:00
```csharp
var data = new { text = "some data", value = 1};
// Pass data as a parameter
var result = await page.EvaluateAsync("data => { window.myApp.use(data); }", data);
```
2020-12-30 18:04:51 -08:00
Wrong:
```js
const data = { text: 'some data', value: 1 };
const result = await page.evaluate(() => {
// There is no |data| in the web page.
window.myApp.use(data);
});
```
2021-03-01 09:18:44 -08:00
```java
Map< String , Object > data = new HashMap< >();
data.put("text", "some data");
data.put("value", 1);
Object result = page.evaluate("() => {\n" +
" // There is no |data| in the web page.\n" +
" window.myApp.use(data);\n" +
"}");
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
data = { 'text': 'some data', 'value': 1 }
result = await page.evaluate("""() => {
# There is no |data| in the web page.
window.myApp.use(data)
}""")
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
data = { 'text': 'some data', 'value': 1 }
result = page.evaluate("""() => {
# There is no |data| in the web page.
window.myApp.use(data)
}""")
```
2021-05-20 04:53:12 +02:00
```csharp
var data = new { text = "some data", value = 1};
// Pass data as a parameter
var result = await page.EvaluateAsync(@"data => {
// There is no |data| in the web page.
window.myApp.use(data);
}");
```
2021-01-17 21:09:40 -08:00
### API reference
2020-12-30 18:04:51 -08:00
- [`method: Page.evaluate` ]
- [`method: Frame.evaluate` ]
- [EvaluationArgument]
< br / >