A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://playwright.dev/python/docs/api/class-browser below:

Browser | Playwright Python

Browser

A Browser is created via browser_type.launch(). An example of using a Browser to create a Page:

from playwright.sync_api import sync_playwright, Playwright

def run(playwright: Playwright):
firefox = playwright.firefox
browser = firefox.launch()
page = browser.new_page()
page.goto("https://example.com")
browser.close()

with sync_playwright() as playwright:
run(playwright)
import asyncio
from playwright.async_api import async_playwright, Playwright

async def run(playwright: Playwright):
firefox = playwright.firefox
browser = await firefox.launch()
page = await browser.new_page()
await page.goto("https://example.com")
await browser.close()

async def main():
async with async_playwright() as playwright:
await run(playwright)
asyncio.run(main())
Methods closeAdded before v1.9 browser.close

In case this browser is obtained using browser_type.launch(), closes the browser and all of its pages (if any were opened).

In case this browser is connected to, clears all created contexts belonging to this browser and disconnects from the browser server.

The Browser object itself is considered to be disposed and cannot be used anymore.

Usage

browser.close()
browser.close(**kwargs)

Arguments

Returns

new_browser_cdp_sessionAdded in: v1.11 browser.new_browser_cdp_session

note

CDP Sessions are only supported on Chromium-based browsers.

Returns the newly created browser session.

Usage

browser.new_browser_cdp_session()

Returns

new_contextAdded before v1.9 browser.new_context

Creates a new browser context. It won't share cookies/cache with other browser contexts.

note

If directly using this method to create BrowserContexts, it is best practice to explicitly close the returned context via browser_context.close() when your code is done with the BrowserContext, and before calling browser.close(). This will ensure the context is closed gracefully and any artifacts—like HARs and videos—are fully flushed and saved.

Usage

browser = playwright.firefox.launch() 

context = browser.new_context()

page = context.new_page()
page.goto("https://example.com")


context.close()
browser.close()
browser = await playwright.firefox.launch() 

context = await browser.new_context()

page = await context.new_page()
await page.goto("https://example.com")


await context.close()
await browser.close()

Arguments

Returns

new_pageAdded before v1.9 browser.new_page

Creates a new page in a new browser context. Closing this page will close the context as well.

This is a convenience API that should only be used for the single-page scenarios and short snippets. Production code and testing frameworks should explicitly create browser.new_context() followed by the browser_context.new_page() to control their exact life times.

Usage

browser.new_page()
browser.new_page(**kwargs)

Arguments

Returns

start_tracingAdded in: v1.11 browser.start_tracing

You can use browser.start_tracing() and browser.stop_tracing() to create a trace file that can be opened in Chrome DevTools performance panel.

Usage

browser.start_tracing(page, path="trace.json")
page.goto("https://www.google.com")
browser.stop_tracing()
await browser.start_tracing(page, path="trace.json")
await page.goto("https://www.google.com")
await browser.stop_tracing()

Arguments

Returns

stop_tracingAdded in: v1.11 browser.stop_tracing

Returns the buffer with trace data.

Usage

Returns

Properties browser_typeAdded in: v1.23 browser.browser_type

Get the browser type (chromium, firefox or webkit) that the browser belongs to.

Usage

Returns

contextsAdded before v1.9 browser.contexts

Returns an array of all open browser contexts. In a newly created browser, this will return zero browser contexts.

Usage

browser = pw.webkit.launch()
print(len(browser.contexts))
context = browser.new_context()
print(len(browser.contexts))
browser = await pw.webkit.launch()
print(len(browser.contexts))
context = await browser.new_context()
print(len(browser.contexts))

Returns

is_connectedAdded before v1.9 browser.is_connected

Indicates that the browser is connected.

Usage

Returns

versionAdded before v1.9 browser.version

Returns the browser version.

Usage

Returns

Events on("disconnected")Added before v1.9 browser.on("disconnected")

Emitted when Browser gets disconnected from the browser application. This might happen because of one of the following:

Usage

browser.on("disconnected", handler)

Event data


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4