Class Browser
Provides methods to interact with a browser in Chromium.
Inheritance
Implements
Namespace: PuppeteerSharp
Assembly: PuppeteerSharp.dll
Syntax
public class Browser : IDisposable
Examples
An example of using a Browser to create a Page:
var browser = await Puppeteer.LaunchAsync(new LaunchOptions());
var page = await browser.NewPageAsync();
await page.GoToAsync("https://example.com");
await browser.CloseAsync();
An example of disconnecting from and reconnecting to a Browser:
var browser = await Puppeteer.LaunchAsync(new LaunchOptions());
var browserWSEndpoint = browser.WebSocketEndpoint;
browser.Disconnect();
var browser2 = await Puppeteer.ConnectAsync(new ConnectOptions { BrowserWSEndpoint = browserWSEndpoint });
await browser2.CloseAsync();
Properties
| Request doc improvement View SourceDefaultContext
Returns the default browser context. The default browser context can not be closed.
Declaration
public BrowserContext DefaultContext { get; }
Property Value
Type | Description |
---|---|
BrowserContext | The default context. |
DefaultWaitForTimeout
Dafault wait time in milliseconds. Defaults to 30 seconds.
Declaration
public int DefaultWaitForTimeout { get; set; }
Property Value
Type | Description |
---|---|
System.Int32 |
IgnoreHTTPSErrors
Gets or Sets whether to ignore HTTPS errors during navigation
Declaration
public bool IgnoreHTTPSErrors { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsClosed
Gets a value indicating if the browser is closed
Declaration
public bool IsClosed { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsConnected
Indicates that the browser is connected.
Declaration
public bool IsConnected { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
Process
Gets the spawned browser process. Returns null
if the browser instance was created with ConnectAsync(ConnectOptions, ILoggerFactory) method.
Declaration
public Process Process { get; }
Property Value
Type | Description |
---|---|
System.Diagnostics.Process |
Target
A target associated with the browser.
Declaration
public Target Target { get; }
Property Value
Type | Description |
---|---|
Target |
WebSocketEndpoint
Gets the Browser websocket url
Declaration
public string WebSocketEndpoint { get; }
Property Value
Type | Description |
---|---|
System.String |
Remarks
Browser websocket endpoint which can be used as an argument to ConnectAsync(ConnectOptions, ILoggerFactory).
The format is ws://${host}:${port}/devtools/browser/[id]
You can find the webSocketDebuggerUrl
from http://${host}:${port}/json/version
.
Learn more about the devtools protocol https://chromedevtools.github.io/devtools-protocol
and the browser endpoint https://chromedevtools.github.io/devtools-protocol/#how-do-i-access-the-browser-target
Methods
| Request doc improvement View SourceBrowserContexts()
Returns an array of all open BrowserContext. In a newly created browser, this will return a single instance of BrowserContext
Declaration
public BrowserContext[] BrowserContexts()
Returns
Type | Description |
---|---|
BrowserContext[] | An array of BrowserContext objects |
CloseAsync()
Closes Chromium and all of its pages (if any were opened). The browser object itself is considered disposed and cannot be used anymore
Declaration
public Task CloseAsync()
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Task |
CreateIncognitoBrowserContextAsync()
Creates a new incognito browser context. This won't share cookies/cache with other browser contexts.
Declaration
public async Task<BrowserContext> CreateIncognitoBrowserContextAsync()
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<BrowserContext> | Task which resolves to a new BrowserContext object |
Examples
using(var browser = await Puppeteer.LaunchAsync(new LaunchOptions()))
{
// Create a new incognito browser context.
var context = await browser.CreateIncognitoBrowserContextAsync();
// Create a new page in a pristine context.
var page = await context.NewPageAsync();
// Do stuff
await page.GoToAsync("https://example.com");
}
|
Request doc improvement
View Source
Disconnect()
Disconnects Puppeteer from the browser, but leaves the process running. After calling Disconnect(), the browser object is considered disposed and cannot be used anymore
Declaration
public void Disconnect()
Dispose()
Declaration
public void Dispose()
Dispose(Boolean)
Closes PuppeteerSharp.Browser.Connection and any Chromium Process that was created by Puppeteer.
Declaration
protected virtual void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | disposing | Indicates whether disposal was initiated by Dispose() operation. |
DisposeAsync()
Closes PuppeteerSharp.Browser.Connection and any Chromium Process that was created by Puppeteer.
Declaration
public ValueTask DisposeAsync()
Returns
Type | Description |
---|---|
ValueTask | ValueTask |
GetUserAgentAsync()
Gets the browser's original user agent
Declaration
public async Task<string> GetUserAgentAsync()
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.String> | Task which resolves to the browser's original user agent |
Remarks
Pages can override browser user agent with SetUserAgentAsync(String)
GetVersionAsync()
Gets the browser's version
Declaration
public async Task<string> GetVersionAsync()
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.String> | For headless Chromium, this is similar to |
Remarks
the format of GetVersionAsync() might change with future releases of Chromium
NewPageAsync()
Creates a new page
Declaration
public Task<Page> NewPageAsync()
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<Page> | Task which resolves to a new Page object |
PagesAsync()
Returns a Task which resolves to an array of all open pages.
Non visible pages, such as "background_page"
, will not be listed here. You can find them using PageAsync()
Declaration
public async Task<Page[]> PagesAsync()
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<Page[]> | Task which resolves to an array of all open pages inside the Browser. In case of multiple browser contexts, the method will return an array with all the pages in all browser contexts. |
Targets()
Returns An Array of all active targets
Declaration
public Target[] Targets()
Returns
Type | Description |
---|---|
Target[] | An Array of all active targets |
WaitForTargetAsync(Func<Target, Boolean>, WaitForOptions)
This searches for a target in this specific browser context.
await page.EvaluateAsync("() => window.open('https://www.example.com/')");
var newWindowTarget = await browserContext.WaitForTargetAsync((target) => target.Url == "https://www.example.com/");
Declaration
public async Task<Target> WaitForTargetAsync(Func<Target, bool> predicate, WaitForOptions options = null)
Parameters
Type | Name | Description |
---|---|---|
System.Func<Target, System.Boolean> | predicate | A function to be run for every target |
WaitForOptions | options | options |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<Target> | Resolves to the first target found that matches the predicate function. |
Events
| Request doc improvement View SourceClosed
Raised when the Browser gets closed.
Declaration
public event EventHandler Closed
Event Type
Type | Description |
---|---|
System.EventHandler |
Disconnected
Raised when puppeteer gets disconnected from the Chromium instance. This might happen because one of the following
- Chromium is closed or crashed
- Disconnect() method was called
Declaration
public event EventHandler Disconnected
Event Type
Type | Description |
---|---|
System.EventHandler |
TargetChanged
Raised when the url of a target changes
Declaration
public event EventHandler<TargetChangedArgs> TargetChanged
Event Type
Type | Description |
---|---|
System.EventHandler<TargetChangedArgs> |
TargetCreated
Raised when a target is created, for example when a new page is opened by window.open
https://developer.mozilla.org/en-US/docs/Web/API/Window/open or NewPageAsync().
Declaration
public event EventHandler<TargetChangedArgs> TargetCreated
Event Type
Type | Description |
---|---|
System.EventHandler<TargetChangedArgs> |
TargetDestroyed
Raised when a target is destroyed, for example when a page is closed
Declaration
public event EventHandler<TargetChangedArgs> TargetDestroyed
Event Type
Type | Description |
---|---|
System.EventHandler<TargetChangedArgs> |