Interface IBrowser
- Namespace
- Microsoft.Playwright
- Assembly
- Microsoft.Playwright.dll
A Browser is created via LaunchAsync(BrowserTypeLaunchOptions?). An example of using a IBrowser to create a IPage:
using Microsoft.Playwright;
using var playwright = await Playwright.CreateAsync();
var firefox = playwright.Firefox;
var browser = await firefox.LaunchAsync(new() { Headless = false });
var page = await browser.NewPageAsync();
await page.GotoAsync("https://www.bing.com");
await browser.CloseAsync();
public interface IBrowser : IAsyncDisposable
Properties
BrowserType
Get the browser type (chromium, firefox or webkit) that the browser belongs to.
IBrowserType BrowserType { get; }
Property Value
Contexts
Returns an array of all open browser contexts. In a newly created browser, this will return zero browser contexts.
**Usage**
using var playwright = await Playwright.CreateAsync();
var browser = await playwright.Webkit.LaunchAsync();
System.Console.WriteLine(browser.Contexts.Count); // prints "0"
var context = await browser.NewContextAsync();
System.Console.WriteLine(browser.Contexts.Count); // prints "1"
IReadOnlyList<IBrowserContext> Contexts { get; }
Property Value
IsConnected
Indicates that the browser is connected.
bool IsConnected { get; }
Property Value
Version
Returns the browser version.
string Version { get; }
Property Value
Methods
CloseAsync(BrowserCloseOptions?)
In case this browser is obtained using LaunchAsync(BrowserTypeLaunchOptions?), 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 IBrowser object itself is considered to be disposed and cannot be used anymore.
Task CloseAsync(BrowserCloseOptions? options = null)
Parameters
options
BrowserCloseOptionsCall options
Returns
Remarks
This is similar to force quitting the browser. Therefore, you should call CloseAsync(BrowserContextCloseOptions?) on any IBrowserContext's you explicitly created earlier with NewContextAsync(BrowserNewContextOptions?) **before** calling CloseAsync(BrowserCloseOptions?).
NewBrowserCDPSessionAsync()
Returns the newly created browser session.
Task<ICDPSession> NewBrowserCDPSessionAsync()
Returns
Remarks
CDP Sessions are only supported on Chromium-based browsers.
NewContextAsync(BrowserNewContextOptions?)
Creates a new browser context. It won't share cookies/cache with other browser contexts.
**Usage**
using var playwright = await Playwright.CreateAsync();
var browser = await playwright.Firefox.LaunchAsync();
// Create a new incognito browser context.
var context = await browser.NewContextAsync();
// Create a new page in a pristine context.
var page = await context.NewPageAsync(); ;
await page.GotoAsync("https://www.bing.com");
// Gracefully close up everything
await context.CloseAsync();
await browser.CloseAsync();
Task<IBrowserContext> NewContextAsync(BrowserNewContextOptions? options = null)
Parameters
options
BrowserNewContextOptionsCall options
Returns
Remarks
If directly using this method to create IBrowserContexts, it is best
practice to explicitly close the returned context via CloseAsync(BrowserContextCloseOptions?)
when your code is done with the IBrowserContext, and before calling
CloseAsync(BrowserCloseOptions?). This will ensure the context
is closed
gracefully and any artifacts—like HARs and videos—are fully flushed and saved.
NewPageAsync(BrowserNewPageOptions?)
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 NewContextAsync(BrowserNewContextOptions?) followed by the NewPageAsync() to control their exact life times.
Task<IPage> NewPageAsync(BrowserNewPageOptions? options = null)
Parameters
options
BrowserNewPageOptionsCall options
Returns
Events
Disconnected
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 CloseAsync(BrowserCloseOptions?) method was called.
event EventHandler<IBrowser> Disconnected