2021-01-11 09:34:49 -08:00
---
2021-01-15 17:59:19 -08:00
id: intro
2021-08-23 20:10:12 -07:00
title: "Getting started"
2021-01-11 09:34:49 -08:00
---
<!-- TOC -->
2021-07-22 19:17:29 +05:30
2021-02-09 21:44:16 -08:00
- [Release notes ](./release-notes.md )
2021-01-11 09:34:49 -08:00
## Installation
2021-06-03 15:07:43 +05:30
See [system requirements ](#system-requirements ).
### Pip
2021-01-11 09:34:49 -08:00
2021-06-21 19:57:56 +02:00
[](https://pypi.python.org/pypi/playwright/)
2021-06-02 09:23:06 -07:00
```bash
2021-08-23 05:37:00 -07:00
pip install --upgrade pip
2021-05-11 20:47:48 +02:00
pip install playwright
playwright install
2021-01-11 09:34:49 -08:00
```
2021-06-03 15:07:43 +05:30
### Conda
2021-06-21 19:57:56 +02:00
[](https://anaconda.org/Microsoft/playwright)
2021-06-03 15:07:43 +05:30
```bash
conda config --add channels conda-forge
conda config --add channels microsoft
conda install playwright
playwright install
```
2021-08-06 14:02:41 -07:00
These commands download the Playwright package and install browser binaries for Chromium, Firefox and WebKit. To modify this behavior see [installation parameters ](./browsers.md#installing-browsers ).
2021-01-11 09:34:49 -08:00
## Usage
Once installed, you can `import` Playwright in a Python script, and launch any of the 3 browsers (`chromium` , `firefox` and `webkit` ).
2021-01-11 17:04:24 -08:00
```py
from playwright.sync_api import sync_playwright
2021-01-11 09:34:49 -08:00
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
2021-01-12 12:14:27 -08:00
page.goto("http://playwright.dev")
2021-01-11 17:04:24 -08:00
print(page.title())
2021-01-11 09:34:49 -08:00
browser.close()
```
2021-01-12 12:14:27 -08:00
Playwright supports two variations of the API: synchronous and asynchronous. If your modern project uses [asyncio ](https://docs.python.org/3/library/asyncio.html ), you should use async API:
2021-01-11 09:34:49 -08:00
2021-01-13 21:03:35 -08:00
```py
2021-01-11 17:04:24 -08:00
import asyncio
from playwright.async_api import async_playwright
2021-01-11 09:34:49 -08:00
2021-01-11 17:04:24 -08:00
async def main():
async with async_playwright() as p:
browser = await p.chromium.launch()
page = await browser.new_page()
2021-01-12 12:14:27 -08:00
await page.goto("http://playwright.dev")
2021-01-11 17:04:24 -08:00
print(await page.title())
await browser.close()
asyncio.run(main())
2021-01-11 09:34:49 -08:00
```
## First script
In our first script, we will navigate to `whatsmyuseragent.org` and take a screenshot in WebKit.
2021-01-13 21:03:35 -08:00
```py
2021-01-11 17:04:24 -08:00
from playwright.sync_api import sync_playwright
2021-01-11 09:34:49 -08:00
with sync_playwright() as p:
browser = p.webkit.launch()
2021-03-21 09:51:31 -07:00
page = browser.new_page()
2021-01-11 09:34:49 -08:00
page.goto("http://whatsmyuseragent.org/")
page.screenshot(path="example.png")
browser.close()
```
2021-01-20 16:06:26 -08:00
By default, Playwright runs the browsers in headless mode. To see the browser UI, pass the `headless=False` flag while launching the browser. You can also use [`option: slowMo` ] to slow down execution. Learn more in the debugging tools [section ](./debug.md ).
2021-01-11 09:34:49 -08:00
2021-01-13 21:03:35 -08:00
```py
2021-02-26 06:01:10 +01:00
firefox.launch(headless=False, slow_mo=50)
2021-01-11 09:34:49 -08:00
```
## Record scripts
2021-08-23 20:10:12 -07:00
[Command line tools ](./cli.md ) can be used to record user interactions and generate Python code.
2021-01-11 09:34:49 -08:00
2021-06-02 09:23:06 -07:00
```bash
2021-05-11 20:47:48 +02:00
playwright codegen wikipedia.org
2021-01-11 09:34:49 -08:00
```
2021-06-21 19:57:56 +02:00
## With Pytest
See [here ](./test-runners.md ) for Pytest instructions and examples.
## Interactive mode (REPL)
Blocking REPL, as in CLI via Python directly:
```bash
python
```
```py
>>> from playwright.sync_api import sync_playwright
>>> playwright = sync_playwright().start()
# Use playwright.chromium, playwright.firefox or playwright.webkit
# Pass headless=False to launch() to see the browser UI
>>> browser = playwright.chromium.launch()
>>> page = browser.new_page()
>>> page.goto("http://whatsmyuseragent.org/")
>>> page.screenshot(path="example.png")
>>> browser.close()
>>> playwright.stop()
```
Async REPL such as `asyncio` REPL:
```bash
python -m asyncio
```
```py
>>> from playwright.async_api import async_playwright
>>> playwright = await async_playwright().start()
>>> browser = await playwright.chromium.launch()
>>> page = await browser.new_page()
>>> await page.goto("http://whatsmyuseragent.org/")
>>> await page.screenshot(path="example.png")
>>> await browser.close()
>>> await playwright.stop()
```
2021-07-22 19:17:29 +05:30
## Pyinstaller
You can use Playwright with [Pyinstaller ](https://www.pyinstaller.org/ ) to create standalone executables.
```py
# main.py
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto("http://whatsmyuseragent.org/")
page.screenshot(path="example.png")
browser.close()
```
If you want to bundle browsers with the executables:
2021-10-04 15:45:52 +02:00
```bash bash-flavor=bash
2021-07-22 19:17:29 +05:30
PLAYWRIGHT_BROWSERS_PATH=0 playwright install chromium
pyinstaller -F main.py
2021-10-04 15:45:52 +02:00
```
2021-07-22 19:17:29 +05:30
2021-10-04 15:45:52 +02:00
```bash bash-flavor=batch
2021-07-22 19:17:29 +05:30
set PLAYWRIGHT_BROWSERS_PATH=0
playwright install chromium
pyinstaller -F main.py
2021-10-04 15:45:52 +02:00
```
2021-07-22 19:17:29 +05:30
2021-10-04 15:45:52 +02:00
```bash bash-flavor=powershell
2021-07-22 19:17:29 +05:30
$env:PLAYWRIGHT_BROWSERS_PATH="0"
playwright install chromium
pyinstaller -F main.py
```
:::note
Bundling the browsers with the executables will generate bigger binaries.
It is recommended to only bundle the browsers you use.
:::
2021-06-21 19:57:56 +02:00
## Known issues
### `time.sleep()` leads to outdated state
You should use `page.wait_for_timeout(5000)` instead of `time.sleep(5)` and it is better to not wait for a timeout at all, but sometimes it is useful for debugging. In these cases, use our wait method instead of the `time` module. This is because we internally rely on asynchronous operations and when using `time.sleep(5)` they can't get processed correctly.
2021-08-24 20:34:25 +05:30
### incompatible with `SelectorEventLoop` of `asyncio` on Windows
Playwright runs the driver in a subprocess, so it requires `ProactorEventLoop` of `asyncio` on Windows because `SelectorEventLoop` does not supports async subprocesses.
On Windows Python 3.7, Playwright sets the default event loop to `ProactorEventLoop` as it is default on Python 3.8+.
2021-09-27 15:52:16 +05:30
### Threading
Playwright's API is not thread-safe. If you are using Playwright in a multi-threaded environment, you should create a playwright instance per thread. See [threading issue ](https://github.com/microsoft/playwright-python/issues/623 ) for more details.
2021-01-11 09:34:49 -08:00
## System requirements
2021-04-30 16:44:30 +02:00
Playwright requires Python 3.7 or above. The browser binaries for Chromium,
2021-01-11 09:34:49 -08:00
Firefox and WebKit work across the 3 platforms (Windows, macOS, Linux):
2021-04-30 16:44:30 +02:00
### Windows
Works with Windows and Windows Subsystem for Linux (WSL).
### macOS
Requires 10.14 (Mojave) or above.
### Linux
Depending on your Linux distribution, you might need to install additional
dependencies to run the browsers.
:::note
Only Ubuntu 18.04 and Ubuntu 20.04 are officially supported.
:::
2021-08-23 20:10:12 -07:00
See also in the [Command line tools ](./cli.md#install-system-dependencies )
2021-04-30 16:44:30 +02:00
which has a command to install all necessary dependencies automatically for Ubuntu
LTS releases.