Playwright文档 - Browser(浏览器)
class: Browser
- since: v1.8
- extends: [EventEmitter]
A Browser is created via [method: BrowserType.launch
]. An example of using a [Browser] to create a [Page]:
1const { firefox } = require('playwright'); // Or 'chromium' or 'webkit'. 2 3(async () => { 4 const browser = await firefox.launch(); 5 const page = await browser.newPage(); 6 await page.goto('https://example.com'); 7 await browser.close(); 8})(); 9
1import com.microsoft.playwright.*; 2 3public class Example { 4 public static void main(String[] args) { 5 try (Playwright playwright = Playwright.create()) { 6 BrowserType firefox = playwright.firefox() 7 Browser browser = firefox.launch(); 8 Page page = browser.newPage(); 9 page.navigate('https://example.com'); 10 browser.close(); 11 } 12 } 13} 14
1import asyncio 2from playwright.async_api import async_playwright, Playwright 3 4async def run(playwright: Playwright): 5 firefox = playwright.firefox 6 browser = await firefox.launch() 7 page = await browser.new_page() 8 await page.goto("https://example.com") 9 await browser.close() 10 11async def main(): 12 async with async_playwright() as playwright: 13 await run(playwright) 14asyncio.run(main()) 15
1from playwright.sync_api import sync_playwright, Playwright 2 3def run(playwright: Playwright): 4 firefox = playwright.firefox 5 browser = firefox.launch() 6 page = browser.new_page() 7 page.goto("https://example.com") 8 browser.close() 9 10with sync_playwright() as playwright: 11 run(playwright) 12
1using Microsoft.Playwright; 2 3using var playwright = await Playwright.CreateAsync(); 4var firefox = playwright.Firefox; 5var browser = await firefox.LaunchAsync(new() { Headless = false }); 6var page = await browser.NewPageAsync(); 7await page.GotoAsync("https://www.bing.com"); 8await browser.CloseAsync(); 9
event: Browser.disconnected
- since: v1.8
- argument: <[Browser]>
Emitted when Browser gets disconnected from the browser application. This might happen because of one of the following:
- Browser application is closed or crashed.
- The [
method: Browser.close
] method was called.
method: Browser.browserType
- since: v1.23
- returns: <[BrowserType]>
Get the browser type (chromium, firefox or webkit) that the browser belongs to.
async method: Browser.close
- since: v1.8
In case this browser is obtained using [method: BrowserType.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.
:::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
].
:::
The [Browser] object itself is considered to be disposed and cannot be used anymore.
method: Browser.contexts
- since: v1.8
- returns: <[Array]<[BrowserContext]>>
Returns an array of all open browser contexts. In a newly created browser, this will return zero browser contexts.
Usage
1const browser = await pw.webkit.launch(); 2console.log(browser.contexts().length); // prints `0` 3 4const context = await browser.newContext(); 5console.log(browser.contexts().length); // prints `1` 6
1Browser browser = pw.webkit().launch(); 2System.out.println(browser.contexts().size()); // prints "0" 3BrowserContext context = browser.newContext(); 4System.out.println(browser.contexts().size()); // prints "1" 5
1browser = await pw.webkit.launch() 2print(len(browser.contexts())) # prints `0` 3context = await browser.new_context() 4print(len(browser.contexts())) # prints `1` 5
1browser = pw.webkit.launch() 2print(len(browser.contexts())) # prints `0` 3context = browser.new_context() 4print(len(browser.contexts())) # prints `1` 5
1using var playwright = await Playwright.CreateAsync(); 2var browser = await playwright.Webkit.LaunchAsync(); 3System.Console.WriteLine(browser.Contexts.Count); // prints "0" 4var context = await browser.NewContextAsync(); 5System.Console.WriteLine(browser.Contexts.Count); // prints "1" 6
method: Browser.isConnected
- since: v1.8
- returns: <[boolean]>
Indicates that the browser is connected.
async method: Browser.newBrowserCDPSession
- since: v1.11
- returns: <[CDPSession]>
:::note CDP Sessions are only supported on Chromium-based browsers. :::
Returns the newly created browser session.
async method: Browser.newContext
- since: v1.8
- returns: <[BrowserContext]>
Creates a new browser context. It won't share cookies/cache with other browser contexts.
:::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],
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.
:::
Usage
1(async () => { 2 const browser = await playwright.firefox.launch(); // Or 'chromium' or 'webkit'. 3 // Create a new incognito browser context. 4 const context = await browser.newContext(); 5 // Create a new page in a pristine context. 6 const page = await context.newPage(); 7 await page.goto('https://example.com'); 8 9 // Gracefully close up everything 10 await context.close(); 11 await browser.close(); 12})(); 13
1Browser browser = playwright.firefox().launch(); // Or 'chromium' or 'webkit'. 2// Create a new incognito browser context. 3BrowserContext context = browser.newContext(); 4// Create a new page in a pristine context. 5Page page = context.newPage(); 6page.navigate('https://example.com'); 7 8// Graceful close up everything 9context.close(); 10browser.close(); 11
1browser = await playwright.firefox.launch() # or "chromium" or "webkit". 2# create a new incognito browser context. 3context = await browser.new_context() 4# create a new page in a pristine context. 5page = await context.new_page() 6await page.goto("https://example.com") 7 8# gracefully close up everything 9await context.close() 10await browser.close() 11
1browser = playwright.firefox.launch() # or "chromium" or "webkit". 2# create a new incognito browser context. 3context = browser.new_context() 4# create a new page in a pristine context. 5page = context.new_page() 6page.goto("https://example.com") 7 8# gracefully close up everything 9context.close() 10browser.close() 11
1using var playwright = await Playwright.CreateAsync(); 2var browser = await playwright.Firefox.LaunchAsync(); 3// Create a new incognito browser context. 4var context = await browser.NewContextAsync(); 5// Create a new page in a pristine context. 6var page = await context.NewPageAsync(); ; 7await page.GotoAsync("https://www.bing.com"); 8 9// Gracefully close up everything 10await context.CloseAsync(); 11await browser.CloseAsync(); 12
option: Browser.newContext.-inline- = %%-shared-context-params-list-v1.8-%%
- since: v1.8
option: Browser.newContext.proxy = %%-context-option-proxy-%%
- since: v1.8
option: Browser.newContext.storageState = %%-js-python-context-option-storage-state-%%
- since: v1.8
option: Browser.newContext.storageState = %%-csharp-java-context-option-storage-state-%%
- since: v1.8
option: Browser.newContext.storageStatePath = %%-csharp-java-context-option-storage-state-path-%%
- since: v1.9
async method: Browser.newPage
- since: v1.8
- returns: <[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 [method: Browser.newContext
] followed by the
[method: BrowserContext.newPage
] to control their exact life times.
option: Browser.newPage.-inline- = %%-shared-context-params-list-v1.8-%%
- since: v1.8
option: Browser.newPage.proxy = %%-context-option-proxy-%%
- since: v1.8
option: Browser.newPage.storageState = %%-js-python-context-option-storage-state-%%
- since: v1.8
option: Browser.newPage.storageState = %%-csharp-java-context-option-storage-state-%%
- since: v1.8
option: Browser.newPage.storageStatePath = %%-csharp-java-context-option-storage-state-path-%%
- since: v1.9
async method: Browser.startTracing
- since: v1.11
- langs: java, js, python
:::note This API controls Chromium Tracing which is a low-level chromium-specific debugging tool. API to control Playwright Tracing could be found here. :::
You can use [method: Browser.startTracing
] and [method: Browser.stopTracing
] to create a trace file that can
be opened in Chrome DevTools performance panel.
Usage
1await browser.startTracing(page, { path: 'trace.json' }); 2await page.goto('https://www.google.com'); 3await browser.stopTracing(); 4
1browser.startTracing(page, new Browser.StartTracingOptions() 2 .setPath(Paths.get("trace.json"))); 3page.goto('https://www.google.com'); 4browser.stopTracing(); 5
1await browser.start_tracing(page, path="trace.json") 2await page.goto("https://www.google.com") 3await browser.stop_tracing() 4
1browser.start_tracing(page, path="trace.json") 2page.goto("https://www.google.com") 3browser.stop_tracing() 4
param: Browser.startTracing.page
- since: v1.11
page
?<[Page]>
Optional, if specified, tracing includes screenshots of the given page.
option: Browser.startTracing.path
- since: v1.11
path
<[path]>
A path to write the trace file to.
option: Browser.startTracing.screenshots
- since: v1.11
screenshots
<[boolean]>
captures screenshots in the trace.
option: Browser.startTracing.categories
- since: v1.11
categories
<[Array]<[string]>>
specify custom categories to use instead of default.
async method: Browser.stopTracing
- since: v1.11
- langs: java, js, python
- returns: <[Buffer]>
:::note This API controls Chromium Tracing which is a low-level chromium-specific debugging tool. API to control Playwright Tracing could be found here. :::
Returns the buffer with trace data.
method: Browser.version
- since: v1.8
- returns: <[string]>
Returns the browser version.