BrowserType provides methods to launch a specific browser instance or connect to an existing one. The following is a typical example of using Playwright to drive automation:
const { chromium } = require('playwright'); // Or 'firefox' or 'webkit'.
(async () => {
const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
// other actions...
await browser.close();
})();
import asyncio
from playwright.async_api import async_playwright
async def run(playwright):
chromium = playwright.chromium
browser = await chromium.launch()
page = await browser.new_page()
await page.goto("https://example.com")
# other actions...
await browser.close()
async def main():
async with async_playwright() as playwright:
await run(playwright)
asyncio.run(main())
from playwright.sync_api import sync_playwright
def run(playwright):
chromium = playwright.chromium
browser = chromium.launch()
page = browser.new_page()
page.goto("https://example.com")
# other actions...
browser.close()
with sync_playwright() as playwright:
run(playwright)
- langs: js
- returns: <[Browser]>
This methods attaches Playwright to an existing browser instance.
params
<[Object]>wsEndpoint
<[string]> A browser websocket endpoint to connect to.slowMo
<[float]> Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on. Defaults to 0.logger
<[Logger]> Logger sink for Playwright logging. Optional.timeout
<[float]> Maximum time in milliseconds to wait for the connection to be established. Defaults to30000
(30 seconds). Pass0
to disable timeout.
- returns: <[string]>
A path where Playwright expects to find a bundled browser executable.
- returns: <[Browser]>
Returns the browser instance.
You can use [option: ignoreDefaultArgs
] to filter out --mute-audio
from default arguments:
const browser = await chromium.launch({ // Or 'firefox' or 'webkit'.
ignoreDefaultArgs: ['--mute-audio']
});
browser = await playwright.chromium.launch( # or "firefox" or "webkit".
ignore_default_args=["--mute-audio"]
)
browser = playwright.chromium.launch( # or "firefox" or "webkit".
ignore_default_args=["--mute-audio"]
)
Chromium-only Playwright can also be used to control the Google Chrome or Microsoft Edge browsers, but it works best with the version of Chromium it is bundled with. There is no guarantee it will work with any other version. Use [
option: executablePath
] option with extreme caution.If Google Chrome (rather than Chromium) is preferred, a Chrome Canary or Dev Channel build is suggested.
Stock browsers like Google Chrome and Microsoft Edge are suitable for tests that require proprietary media codecs for video playback. See this article for other differences between Chromium and Chrome. This article describes some differences for Linux users.
headless
<[boolean]>
Whether to run browser in headless mode. More details for
Chromium and
Firefox. Defaults to true
unless the
[option: devtools
] option is true
.
executablePath
<[path]>
Path to a browser executable to run instead of the bundled one. If [option: executablePath
] is a relative path, then
it is resolved relative to the current working directory. Note that Playwright only works with the bundled Chromium,
Firefox or WebKit, use at your own risk.
args
<[Array]<[string]>>
Additional arguments to pass to the browser instance. The list of Chromium flags can be found here.
downloadsPath
<[path]>
If specified, accepted downloads are downloaded into this directory. Otherwise, temporary directory is created and is deleted when browser is closed.
chromiumSandbox
<[boolean]>
Enable Chromium sandboxing. Defaults to false
.
- langs: js, python
firefoxUserPrefs
<[Object]<[string], [string]|[float]|[boolean]>>
Firefox user preferences. Learn more about the Firefox user preferences at
about:config
.
- langs: csharp, java
firefoxUserPrefs
<[Object]<[string], [any]>>
Firefox user preferences. Learn more about the Firefox user preferences at
about:config
.
handleSIGINT
<[boolean]>
Close the browser process on Ctrl-C. Defaults to true
.
handleSIGTERM
<[boolean]>
Close the browser process on SIGTERM. Defaults to true
.
handleSIGHUP
<[boolean]>
Close the browser process on SIGHUP. Defaults to true
.
- langs: js
logger
<[Logger]>
Logger sink for Playwright logging.
timeout
<[float]>
Maximum time in milliseconds to wait for the browser instance to start. Defaults to 30000
(30 seconds). Pass 0
to
disable timeout.
devtools
<[boolean]>
Chromium-only Whether to auto-open a Developer Tools panel for each tab. If this option is true
, the
[option: headless
] option will be set false
.
slowMo
<[float]>
Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on.
- returns: <[BrowserContext]>
Returns the persistent browser context instance.
Launches browser that uses persistent storage located at [param: userDataDir
] and returns the only context. Closing
this context will automatically close the browser.
userDataDir
<[path]>
Path to a User Data Directory, which stores browser session data like cookies and local storage. More details for
Chromium and
Firefox.
Note that Chromium's user data directory is the parent directory of the "Profile Path" seen at chrome://version
.
headless
<[boolean]>
Whether to run browser in headless mode. More details for
Chromium and
Firefox. Defaults to true
unless the
[option: devtools
] option is true
.
executablePath
<[path]>
Path to a browser executable to run instead of the bundled one. If [option: executablePath
] is a relative path, then
it is resolved relative to the current working directory. BEWARE: Playwright is only guaranteed to work with the
bundled Chromium, Firefox or WebKit, use at your own risk.
args
<[Array]<[string]>>
Additional arguments to pass to the browser instance. The list of Chromium flags can be found here.
option: BrowserType.launchPersistentContext.ignoreDefaultArgs = %%-browser-option-ignoredefaultargs-%%
downloadsPath
<[path]>
If specified, accepted downloads are downloaded into this directory. Otherwise, temporary directory is created and is deleted when browser is closed.
chromiumSandbox
<[boolean]>
Enable Chromium sandboxing. Defaults to true
.
handleSIGINT
<[boolean]>
Close the browser process on Ctrl-C. Defaults to true
.
handleSIGTERM
<[boolean]>
Close the browser process on SIGTERM. Defaults to true
.
handleSIGHUP
<[boolean]>
Close the browser process on SIGHUP. Defaults to true
.
timeout
<[float]>
Maximum time in milliseconds to wait for the browser instance to start. Defaults to 30000
(30 seconds). Pass 0
to
disable timeout.
devtools
<[boolean]>
Chromium-only Whether to auto-open a Developer Tools panel for each tab. If this option is true
, the
[option: headless
] option will be set false
.
slowMo
<[float]>
Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on. Defaults to 0.
- langs: js
- returns: <[BrowserServer]>
Returns the browser app instance.
Launches browser server that client can connect to. An example of launching a browser executable and connecting to it later:
const { chromium } = require('playwright'); // Or 'webkit' or 'firefox'.
(async () => {
const browserServer = await chromium.launchServer();
const wsEndpoint = browserServer.wsEndpoint();
// Use web socket endpoint later to establish a connection.
const browser = await chromium.connect({ wsEndpoint });
// Close browser instance.
await browserServer.close();
})();
headless
<[boolean]>
Whether to run browser in headless mode. More details for
Chromium and
Firefox. Defaults to true
unless the
[option: devtools
] option is true
.
port
<[int]>
Port to use for the web socket. Defaults to 0 that picks any available port.
executablePath
<[path]>
Path to a browser executable to run instead of the bundled one. If [option: executablePath
] is a relative path, then
it is resolved relative to the current working directory. BEWARE: Playwright is only guaranteed to work with the
bundled Chromium, Firefox or WebKit, use at your own risk.
args
<[Array]<[string]>>
Additional arguments to pass to the browser instance. The list of Chromium flags can be found here.
downloadsPath
<[path]>
If specified, accepted downloads are downloaded into this directory. Otherwise, temporary directory is created and is deleted when browser is closed.
chromiumSandbox
<[boolean]>
Enable Chromium sandboxing. Defaults to true
.
firefoxUserPrefs
<[Object]<[string], [string]|[float]|[boolean]>>
Firefox user preferences. Learn more about the Firefox user preferences at
about:config
.
handleSIGINT
<[boolean]>
Close the browser process on Ctrl-C. Defaults to true
.
handleSIGTERM
<[boolean]>
Close the browser process on SIGTERM. Defaults to true
.
handleSIGHUP
<[boolean]>
Close the browser process on SIGHUP. Defaults to true
.
- langs: js
logger
<[Logger]>
Logger sink for Playwright logging.
timeout
<[float]>
Maximum time in milliseconds to wait for the browser instance to start. Defaults to 30000
(30 seconds). Pass 0
to
disable timeout.
devtools
<[boolean]>
Chromium-only Whether to auto-open a Developer Tools panel for each tab. If this option is true
, the
[option: headless
] option will be set false
.
- returns: <[string]>
Returns browser name. For example: 'chromium'
, 'webkit'
or 'firefox'
.