2021-02-21 18:36:39 -08:00
---
id: inspector
title: "Inspector"
---
Playwright Inspector is a GUI tool that helps authoring and debugging Playwright scripts.
2021-02-22 16:37:16 -08:00
< img width = "712" alt = "Playwright Inspector" src = "https://user-images.githubusercontent.com/883973/108614092-8c478a80-73ac-11eb-9597-67dfce110e00.png" > < / img >
2021-02-21 18:36:39 -08:00
<!-- TOC -->
## Open Playwright Inspector
There are several ways of opening Playwright Inspector:
- Set the `PWDEBUG` environment variable to run your scripts in debug mode. This
configures Playwright for debugging and opens the inspector.
2021-10-04 15:45:52 +02:00
```bash bash-flavor=bash lang=js
2021-05-11 20:47:48 +02:00
PWDEBUG=1 npm run test
2021-10-04 15:45:52 +02:00
```
2021-02-21 18:36:39 -08:00
2021-10-04 15:45:52 +02:00
```bash bash-flavor=batch lang=js
2021-05-11 20:47:48 +02:00
set PWDEBUG=1
npm run test
2021-10-04 15:45:52 +02:00
```
2021-05-25 07:10:22 +02:00
2021-10-04 15:45:52 +02:00
```bash bash-flavor=powershell lang=js
2021-05-25 07:10:22 +02:00
$env:PWDEBUG=1
npm run test
2021-02-21 18:36:39 -08:00
```
2021-10-04 15:45:52 +02:00
```bash bash-flavor=bash lang=java
2021-05-11 20:47:48 +02:00
PWDEBUG=1 PLAYWRIGHT_JAVA_SRC=< java src root > mvn test
2021-10-04 15:45:52 +02:00
```
2021-03-03 15:17:22 -08:00
2021-10-04 15:45:52 +02:00
```bash bash-flavor=batch lang=java
2021-05-11 20:47:48 +02:00
set PLAYWRIGHT_JAVA_SRC=< java src root >
set PWDEBUG=1
mvn test
2021-10-04 15:45:52 +02:00
```
2021-05-25 07:10:22 +02:00
2021-10-04 15:45:52 +02:00
```bash bash-flavor=powershell lang=java
2021-05-25 07:10:22 +02:00
$env:PLAYWRIGHT_JAVA_SRC="< java src root > "
$env:PWDEBUG=1
mvn test
2021-03-03 15:17:22 -08:00
```
2021-10-04 15:45:52 +02:00
```bash bash-flavor=bash lang=python
2021-05-11 20:47:48 +02:00
PWDEBUG=1 pytest -s
2021-10-04 15:45:52 +02:00
```
2021-02-21 18:36:39 -08:00
2021-10-04 15:45:52 +02:00
```bash bash-flavor=batch lang=python
2021-05-11 20:47:48 +02:00
set PWDEBUG=1
pytest -s
2021-10-04 15:45:52 +02:00
```
2021-05-25 07:10:22 +02:00
2021-10-04 15:45:52 +02:00
```bash bash-flavor=powershell lang=python
2021-05-25 07:10:22 +02:00
$env:PWDEBUG=1
pytest -s
2021-02-21 18:36:39 -08:00
```
2021-04-20 15:58:34 -07:00
Additional useful defaults are configured when `PWDEBUG=1` is set:
2021-02-21 18:36:39 -08:00
- Browsers launch in the headed mode
- Default timeout is set to 0 (= no timeout)
- Call [`method: Page.pause` ] method from your script when running in headed browser.
```js
// Pause on the following line.
await page.pause();
```
2021-03-01 09:18:44 -08:00
```java
// Pause on the following line.
page.pause();
```
2021-02-21 18:36:39 -08:00
```python async
# Pause on the following line.
await page.pause()
```
```python sync
# Pause on the following line.
page.pause()
```
2021-05-20 08:20:21 -07:00
```csharp
// Pause on the following line.
await page.PauseAsync();
```
2021-02-21 18:36:39 -08:00
- Use `open` or `codegen` commands in the Playwright [CLI ](./cli.md ):
2021-06-02 09:23:06 -07:00
```bash js
2021-05-11 20:47:48 +02:00
npx playwright codegen wikipedia.org
2021-02-21 18:36:39 -08:00
```
2021-06-02 09:23:06 -07:00
```bash java
2021-05-11 20:47:48 +02:00
mvn exec:java -e -Dexec.mainClass=com.microsoft.playwright.CLI -Dexec.args="codegen wikipedia.org"
2021-03-05 10:13:02 -08:00
```
2021-06-02 09:23:06 -07:00
```bash python
2021-05-11 20:47:48 +02:00
playwright codegen wikipedia.org
2021-02-21 18:36:39 -08:00
```
## Stepping through the Playwright script
2021-04-20 15:58:34 -07:00
When `PWDEBUG=1` is set, Playwright Inspector window will be opened and the script will be
2021-02-21 18:36:39 -08:00
paused on the first Playwright statement:
2021-02-22 16:37:16 -08:00
< img width = "557" alt = "Paused on line" src = "https://user-images.githubusercontent.com/883973/108614337-71761580-73ae-11eb-9f61-3d29c52c9520.png" > < / img >
2021-02-21 18:36:39 -08:00
Now we know what action is about to be performed and we can look into the details on that
action. For example, when stopped on an input action such as `click` , the exact point Playwright is about to click is highlighted with the large red dot on the inspected page:
2021-02-22 16:37:16 -08:00
< img width = "344" alt = "Red dot on inspected page" src = "https://user-images.githubusercontent.com/883973/108614363-b69a4780-73ae-11eb-8f5e-51f9c91ec9b4.png" > < / img >
2021-02-21 18:36:39 -08:00
By the time Playwright has paused on that click action, it has already performed actionability checks that can be found in the log:
2021-02-22 16:37:16 -08:00
< img width = "712" alt = "Action log" src = "https://user-images.githubusercontent.com/883973/108614564-72a84200-73b0-11eb-9de2-828b28d78b36.png" > < / img >
2021-02-21 18:36:39 -08:00
If actionability can't be reached, it'll show action as pending:
2021-04-20 15:58:34 -07:00
< img width = "712" alt = "Pending action" src = "https://user-images.githubusercontent.com/883973/108614840-e6e3e500-73b2-11eb-998f-0cf31b2aa9a2.png" > < / img >
2021-02-21 18:36:39 -08:00
You can step over each action using the "Step over" action or resume script without further pauses:
2021-02-22 16:37:16 -08:00
< center > < img width = "98" alt = "Stepping toolbar" src = "https://user-images.githubusercontent.com/883973/108614389-f9f4b600-73ae-11eb-8df2-8d9ce9da5d5c.png" > < / img > < / center >
2021-02-21 18:36:39 -08:00
2021-04-20 15:58:34 -07:00
## Using Browser Developer Tools
You can use browser developer tools in Chromium, Firefox and WebKit while running
a Playwright script, with or without Playwright inspector. Developer tools help to:
* Inspect the DOM tree
* **See console logs** during execution (or learn how to [read logs via API ](./verification.md#console-logs ))
* Check **network activity** and other developer tools features
:::note
**For WebKit**: launching WebKit Inspector during the execution will
prevent the Playwright script from executing any further.
:::
2021-02-21 18:36:39 -08:00
## Debugging Selectors
- Click the Explore button to hover over elements in the screen and click them to
automatically generate selectors for those elements.
- To verify where selector points, paste it into the inspector input field:
2021-04-20 15:58:34 -07:00
< img width = "602" alt = "Selectors toolbar" src = "https://user-images.githubusercontent.com/883973/108614696-ad5eaa00-73b1-11eb-81f5-9eebe62543a2.png" > < / img >
2021-02-21 18:36:39 -08:00
2021-09-21 13:46:11 -07:00
You can also use the following API inside the Developer Tools Console of any browser.
< img src = "https://user-images.githubusercontent.com/284612/92536317-37dd9380-f1ee-11ea-875d-daf1b206dd56.png" > < / img >
#### playwright.$(selector)
Query Playwright selector, using the actual Playwright query engine, for example:
```js
> playwright.$('.auth-form >> text=Log in');
< button > Log in< / button >
```
#### playwright.$$(selector)
Same as `playwright.$` , but returns all matching elements.
```js
> playwright.$$('li >> text=John')
> [<li>, <li>, <li>, <li>]
```
#### playwright.inspect(selector)
Reveal element in the Elements panel (if DevTools of the respective browser supports it).
```js
> playwright.inspect('text=Log in')
```
#### playwright.selector(element)
Generates selector for the given element.
```js
> playwright.selector($0)
"div[id="glow-ingress-block"] >> text=/.*Hello.*/"
```
2021-02-21 18:36:39 -08:00
## Recording scripts
2021-09-21 13:46:11 -07:00
At any moment, clicking Record action enables [codegen mode ](./codegen.md ).
2021-02-21 18:36:39 -08:00
Every action on the target page is turned into the generated script:
2021-04-20 15:58:34 -07:00
< img width = "712" alt = "Recorded script" src = "https://user-images.githubusercontent.com/883973/108614897-85704600-73b3-11eb-8bcd-f2e129786c49.png" > < / img >
2021-02-21 18:36:39 -08:00
You can copy entire generated script or clear it using toolbar actions.