Class Browser

Provides methods to interact with a browser in Chromium.

Inheritance
System.Object
Browser
Implements
System.IDisposable
IAsyncDisposable
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 Source

DefaultContext

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.

| Request doc improvement View Source

DefaultWaitForTimeout

Dafault wait time in milliseconds. Defaults to 30 seconds.

Declaration
public int DefaultWaitForTimeout { get; set; }
Property Value
Type Description
System.Int32
| Request doc improvement View Source

IgnoreHTTPSErrors

Gets or Sets whether to ignore HTTPS errors during navigation

Declaration
public bool IgnoreHTTPSErrors { get; set; }
Property Value
Type Description
System.Boolean
| Request doc improvement View Source

IsClosed

Gets a value indicating if the browser is closed

Declaration
public bool IsClosed { get; }
Property Value
Type Description
System.Boolean
| Request doc improvement View Source

IsConnected

Indicates that the browser is connected.

Declaration
public bool IsConnected { get; }
Property Value
Type Description
System.Boolean
| Request doc improvement View Source

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
| Request doc improvement View Source

Target

A target associated with the browser.

Declaration
public Target Target { get; }
Property Value
Type Description
Target
| Request doc improvement View Source

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 Source

BrowserContexts()

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

| Request doc improvement View Source

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

| Request doc improvement View Source

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()
| Request doc improvement View Source

Dispose()

Declaration
public void Dispose()
| Request doc improvement View Source

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.

| Request doc improvement View Source

DisposeAsync()

Closes PuppeteerSharp.Browser.Connection and any Chromium Process that was created by Puppeteer.

Declaration
public ValueTask DisposeAsync()
Returns
Type Description
ValueTask

ValueTask

| Request doc improvement View Source

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)

| Request doc improvement View Source

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 HeadlessChrome/61.0.3153.0. For non-headless, this is similar to Chrome/61.0.3153.0

Remarks

the format of GetVersionAsync() might change with future releases of Chromium

| Request doc improvement View Source

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

| Request doc improvement View Source

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.

| Request doc improvement View Source

Targets()

Returns An Array of all active targets

Declaration
public Target[] Targets()
Returns
Type Description
Target[]

An Array of all active targets

| Request doc improvement View Source

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 Source

Closed

Raised when the Browser gets closed.

Declaration
public event EventHandler Closed
Event Type
Type Description
System.EventHandler
| Request doc improvement View Source

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
| Request doc improvement View Source

TargetChanged

Raised when the url of a target changes

Declaration
public event EventHandler<TargetChangedArgs> TargetChanged
Event Type
Type Description
System.EventHandler<TargetChangedArgs>
| Request doc improvement View Source

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>
| Request doc improvement View Source

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>

Implements

System.IDisposable
IAsyncDisposable
  • Request doc improvement
  • View Source
Generated by DocFX

www.hardkoded.com

Back to top