class Playwright::Browser
“‘
run(playwright)
with sync_playwright() as playwright:
browser.close()
page.goto(“example.com”)
page = browser.new_page()
browser = firefox.launch()
firefox = playwright.firefox
def run(playwright: Playwright):
from playwright.sync_api import sync_playwright, Playwright
“`python sync
A Browser is created via [`method: BrowserType.launch`]. An example of using a `Browser` to create a `Page`:
- extends: [EventEmitter]
def browser_type
def browser_type wrap_impl(@impl.browser_type) end
def close(reason: nil)
**NOTE**: This is similar to force quitting the browser. Therefore, you should call [`method: BrowserContext.close`] on any `BrowserContext`'s you explicitly created earlier with [`method: Browser.newContext`] **before** calling [`method: Browser.close`].
browser server.
In case this browser is connected to, clears all created contexts belonging to this browser and disconnects from the
were opened).
In case this browser is obtained using [`method: BrowserType.launch`], closes the browser and all of its pages (if any
def close(reason: nil) wrap_impl(@impl.close(reason: unwrap_impl(reason))) end
def connected?
def connected? wrap_impl(@impl.connected?) end
def contexts
print(len(browser.contexts())) # prints `1`
context = browser.new_context()
print(len(browser.contexts())) # prints `0`
browser = pw.webkit.launch()
```python sync
**Usage**
Returns an array of all open browser contexts. In a newly created browser, this will return zero browser contexts.
def contexts wrap_impl(@impl.contexts) end
def event_emitter_proxy
def event_emitter_proxy _emitter_proxy ||= EventEmitterProxy.new(self, @impl)
def new_browser_cdp_session
**NOTE**: CDP Sessions are only supported on Chromium-based browsers.
def new_browser_cdp_session wrap_impl(@impl.new_browser_cdp_session) end
def new_context(
browser.close()
context.close()
# gracefully close up everything
page.goto("https://example.com")
page = context.new_page()
# create a new page in a pristine context.
context = browser.new_context()
# create a new incognito browser context.
browser = playwright.firefox.launch() # or "chromium" or "webkit".
```python sync
**Usage**
and before calling [`method: Browser.close`]. This will ensure the `context` is closed gracefully and any artifacts—like HARs and videos—are fully flushed and saved.
**NOTE**: If directly using this method to create `BrowserContext`s, it is best practice to explicitly close the returned context via [`method: BrowserContext.close`] when your code is done with the `BrowserContext`,
Creates a new browser context. It won't share cookies/cache with other browser contexts.
def new_context( acceptDownloads: nil, baseURL: nil, bypassCSP: nil, colorScheme: nil, deviceScaleFactor: nil, extraHTTPHeaders: nil, forcedColors: nil, geolocation: nil, hasTouch: nil, httpCredentials: nil, ignoreHTTPSErrors: nil, isMobile: nil, javaScriptEnabled: nil, locale: nil, noViewport: nil, offline: nil, permissions: nil, proxy: nil, record_har_content: nil, record_har_mode: nil, record_har_omit_content: nil, record_har_path: nil, record_har_url_filter: nil, record_video_dir: nil, record_video_size: nil, reducedMotion: nil, screen: nil, serviceWorkers: nil, storageState: nil, strictSelectors: nil, timezoneId: nil, userAgent: nil, viewport: nil, &block) wrap_impl(@impl.new_context(acceptDownloads: unwrap_impl(acceptDownloads), baseURL: unwrap_impl(baseURL), bypassCSP: unwrap_impl(bypassCSP), colorScheme: unwrap_impl(colorScheme), deviceScaleFactor: unwrap_impl(deviceScaleFactor), extraHTTPHeaders: unwrap_impl(extraHTTPHeaders), forcedColors: unwrap_impl(forcedColors), geolocation: unwrap_impl(geolocation), hasTouch: unwrap_impl(hasTouch), httpCredentials: unwrap_impl(httpCredentials), ignoreHTTPSErrors: unwrap_impl(ignoreHTTPSErrors), isMobile: unwrap_impl(isMobile), javaScriptEnabled: unwrap_impl(javaScriptEnabled), locale: unwrap_impl(locale), noViewport: unwrap_impl(noViewport), offline: unwrap_impl(offline), permissions: unwrap_impl(permissions), proxy: unwrap_impl(proxy), record_har_content: unwrap_impl(record_har_content), record_har_mode: unwrap_impl(record_har_mode), record_har_omit_content: unwrap_impl(record_har_omit_content), record_har_path: unwrap_impl(record_har_path), record_har_url_filter: unwrap_impl(record_har_url_filter), record_video_dir: unwrap_impl(record_video_dir), record_video_size: unwrap_impl(record_video_size), reducedMotion: unwrap_impl(reducedMotion), screen: unwrap_impl(screen), serviceWorkers: unwrap_impl(serviceWorkers), storageState: unwrap_impl(storageState), strictSelectors: unwrap_impl(strictSelectors), timezoneId: unwrap_impl(timezoneId), userAgent: unwrap_impl(userAgent), viewport: unwrap_impl(viewport), &wrap_block_call(block))) end
def new_page(
testing frameworks should explicitly create [`method: Browser.newContext`] followed by the
This is a convenience API that should only be used for the single-page scenarios and short snippets. Production code and
Creates a new page in a new browser context. Closing this page will close the context as well.
def new_page( acceptDownloads: nil, baseURL: nil, bypassCSP: nil, colorScheme: nil, deviceScaleFactor: nil, extraHTTPHeaders: nil, forcedColors: nil, geolocation: nil, hasTouch: nil, httpCredentials: nil, ignoreHTTPSErrors: nil, isMobile: nil, javaScriptEnabled: nil, locale: nil, noViewport: nil, offline: nil, permissions: nil, proxy: nil, record_har_content: nil, record_har_mode: nil, record_har_omit_content: nil, record_har_path: nil, record_har_url_filter: nil, record_video_dir: nil, record_video_size: nil, reducedMotion: nil, screen: nil, serviceWorkers: nil, storageState: nil, strictSelectors: nil, timezoneId: nil, userAgent: nil, viewport: nil, &block) wrap_impl(@impl.new_page(acceptDownloads: unwrap_impl(acceptDownloads), baseURL: unwrap_impl(baseURL), bypassCSP: unwrap_impl(bypassCSP), colorScheme: unwrap_impl(colorScheme), deviceScaleFactor: unwrap_impl(deviceScaleFactor), extraHTTPHeaders: unwrap_impl(extraHTTPHeaders), forcedColors: unwrap_impl(forcedColors), geolocation: unwrap_impl(geolocation), hasTouch: unwrap_impl(hasTouch), httpCredentials: unwrap_impl(httpCredentials), ignoreHTTPSErrors: unwrap_impl(ignoreHTTPSErrors), isMobile: unwrap_impl(isMobile), javaScriptEnabled: unwrap_impl(javaScriptEnabled), locale: unwrap_impl(locale), noViewport: unwrap_impl(noViewport), offline: unwrap_impl(offline), permissions: unwrap_impl(permissions), proxy: unwrap_impl(proxy), record_har_content: unwrap_impl(record_har_content), record_har_mode: unwrap_impl(record_har_mode), record_har_omit_content: unwrap_impl(record_har_omit_content), record_har_path: unwrap_impl(record_har_path), record_har_url_filter: unwrap_impl(record_har_url_filter), record_video_dir: unwrap_impl(record_video_dir), record_video_size: unwrap_impl(record_video_size), reducedMotion: unwrap_impl(reducedMotion), screen: unwrap_impl(screen), serviceWorkers: unwrap_impl(serviceWorkers), storageState: unwrap_impl(storageState), strictSelectors: unwrap_impl(strictSelectors), timezoneId: unwrap_impl(timezoneId), userAgent: unwrap_impl(userAgent), viewport: unwrap_impl(viewport), &wrap_block_call(block))) end
def off(event, callback)
-- inherited from EventEmitter --
def off(event, callback) event_emitter_proxy.off(event, callback) end
def on(event, callback)
-- inherited from EventEmitter --
def on(event, callback) event_emitter_proxy.on(event, callback) end
def once(event, callback)
-- inherited from EventEmitter --
def once(event, callback) event_emitter_proxy.once(event, callback) end
def start_tracing(page: nil, categories: nil, path: nil, screenshots: nil)
browser.stop_tracing()
page.goto("https://www.google.com")
browser.start_tracing(page, path="trace.json")
```python sync
**Usage**
be opened in Chrome DevTools performance panel.
You can use [`method: Browser.startTracing`] and [`method: Browser.stopTracing`] to create a trace file that can
**NOTE**: This API controls [Chromium Tracing](https://www.chromium.org/developers/how-tos/trace-event-profiling-tool) which is a low-level chromium-specific debugging tool. API to control [Playwright Tracing](../trace-viewer) could be found [here](./class-tracing).
def start_tracing(page: nil, categories: nil, path: nil, screenshots: nil) wrap_impl(@impl.start_tracing(page: unwrap_impl(page), categories: unwrap_impl(categories), path: unwrap_impl(path), screenshots: unwrap_impl(screenshots))) end
def stop_tracing
**NOTE**: This API controls [Chromium Tracing](https://www.chromium.org/developers/how-tos/trace-event-profiling-tool) which is a low-level chromium-specific debugging tool. API to control [Playwright Tracing](../trace-viewer) could be found [here](./class-tracing).
def stop_tracing wrap_impl(@impl.stop_tracing) end
def version
def version wrap_impl(@impl.version) end