Skip to main content
Version: 1.15

BrowserType

BrowserType provides methods to launch a specific browser instance or connect to an existing one. The following is a typical example of using Playwright to drive automation:

using Microsoft.Playwright;using System.Threading.Tasks;
class BrowserTypeExamples{    public static async Task Run()    {        using var playwright = await Playwright.CreateAsync();        var chromium = playwright.Chromium;        var browser = await chromium.LaunchAsync();        var page = await browser.NewPageAsync();        await page.GoToAsync("https://www.bing.com");        // other actions        await browser.CloseAsync();    }}

BrowserType.ConnectAsync(wsEndpoint, options)#

  • wsEndpoint <string> A browser websocket endpoint to connect to.#
  • options <BrowserTypeConnectOptions?>
    • Headers <IDictionary?<string, string>> Additional HTTP headers to be sent with web socket connect request. Optional.#
    • SlowMo <double?> Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on. Defaults to 0.#
    • Timeout <double?> Maximum time in milliseconds to wait for the connection to be established. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.#
  • returns: <Browser>#

This methods attaches Playwright to an existing browser instance.

BrowserType.ExecutablePath#

A path where Playwright expects to find a bundled browser executable.

BrowserType.LaunchAsync(options)#

  • options <BrowserTypeLaunchOptions?>
    • Args <IEnumerable?<string>> Additional arguments to pass to the browser instance. The list of Chromium flags can be found here.#
    • Channel <string?> Browser distribution channel. Supported values are "chrome", "chrome-beta", "chrome-dev", "chrome-canary", "msedge", "msedge-beta", "msedge-dev", "msedge-canary". Read more about using Google Chrome and Microsoft Edge.#
    • ChromiumSandbox <bool?> Enable Chromium sandboxing. Defaults to false.#
    • Devtools <bool?> Chromium-only Whether to auto-open a Developer Tools panel for each tab. If this option is true, the headless option will be set false.#
    • DownloadsPath <string?> If specified, accepted downloads are downloaded into this directory. Otherwise, temporary directory is created and is deleted when browser is closed. In either case, the downloads are deleted when the browser context they were created in is closed.#
    • Env <IDictionary?<string, string>> Specify environment variables that will be visible to the browser. Defaults to process.env.#
    • ExecutablePath <string?> Path to a browser executable to run instead of the bundled one. If executablePath is a relative path, then it is resolved relative to the current working directory. Note that Playwright only works with the bundled Chromium, Firefox or WebKit, use at your own risk.#
    • FirefoxUserPrefs <IDictionary?<string, [object]>> Firefox user preferences. Learn more about the Firefox user preferences at about:config.#
    • HandleSIGHUP <bool?> Close the browser process on SIGHUP. Defaults to true.#
    • HandleSIGINT <bool?> Close the browser process on Ctrl-C. Defaults to true.#
    • HandleSIGTERM <bool?> Close the browser process on SIGTERM. Defaults to true.#
    • Headless <bool?> Whether to run browser in headless mode. More details for Chromium and Firefox. Defaults to true unless the devtools option is true.#
    • IgnoreAllDefaultArgs <bool?> If true, Playwright does not pass its own configurations args and only uses the ones from args. Dangerous option; use with care. Defaults to false.#
    • IgnoreDefaultArgs <IEnumerable?<string>> If true, Playwright does not pass its own configurations args and only uses the ones from args. Dangerous option; use with care.#
    • Proxy <Proxy?> Network proxy settings.#
      • Server <string> Proxy to be used for all requests. HTTP and SOCKS proxies are supported, for example http://myproxy.com:3128 or socks5://myproxy.com:3128. Short form myproxy.com:3128 is considered an HTTP proxy.
      • Bypass <string?> Optional coma-separated domains to bypass proxy, for example ".com, chromium.org, .domain.com".
      • Username <string?> Optional username to use if HTTP proxy requires authentication.
      • Password <string?> Optional password to use if HTTP proxy requires authentication.
    • SlowMo <double?> Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on.#
    • Timeout <double?> Maximum time in milliseconds to wait for the browser instance to start. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.#
    • TracesDir <string?> If specified, traces are saved into this directory.#
  • returns: <Browser>#

Returns the browser instance.

You can use ignoreDefaultArgs to filter out --mute-audio from default arguments:

var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions {    IgnoreDefaultArgs = new[] { "--mute-audio" }})

Chromium-only Playwright can also be used to control the Google Chrome or Microsoft Edge browsers, but it works best with the version of Chromium it is bundled with. There is no guarantee it will work with any other version. Use executablePath option with extreme caution.

If Google Chrome (rather than Chromium) is preferred, a Chrome Canary or Dev Channel build is suggested.

Stock browsers like Google Chrome and Microsoft Edge are suitable for tests that require proprietary media codecs for video playback. See this article for other differences between Chromium and Chrome. This article describes some differences for Linux users.

BrowserType.LaunchPersistentContextAsync(userDataDir, options)#

  • userDataDir <string> Path to a User Data Directory, which stores browser session data like cookies and local storage. More details for Chromium and Firefox. Note that Chromium's user data directory is the parent directory of the "Profile Path" seen at chrome://version.#

  • options <BrowserTypeLaunchPersistentContextOptions?>

    • AcceptDownloads <bool?> Whether to automatically download all the attachments. Defaults to false where all the downloads are canceled.#

    • Args <IEnumerable?<string>> Additional arguments to pass to the browser instance. The list of Chromium flags can be found here.#

    • BaseURL <string?> When using Page.GotoAsync(url, options), Page.RouteAsync(url, handler, options), Page.WaitForURLAsync(url, options), Page.RunAndWaitForRequestAsync(action, urlOrPredicate, options), or Page.RunAndWaitForResponseAsync(action, urlOrPredicate, options) it takes the base URL in consideration by using the URL() constructor for building the corresponding URL. Examples:#

      • baseURL: http://localhost:3000 and navigating to /bar.html results in http://localhost:3000/bar.html
      • baseURL: http://localhost:3000/foo/ and navigating to ./bar.html results in http://localhost:3000/foo/bar.html
    • BypassCSP <bool?> Toggles bypassing page's Content-Security-Policy.#

    • Channel <string?> Browser distribution channel. Supported values are "chrome", "chrome-beta", "chrome-dev", "chrome-canary", "msedge", "msedge-beta", "msedge-dev", "msedge-canary". Read more about using Google Chrome and Microsoft Edge.#

    • ChromiumSandbox <bool?> Enable Chromium sandboxing. Defaults to false.#

    • ColorScheme <enum ColorScheme { Light, Dark, NoPreference }?> Emulates 'prefers-colors-scheme' media feature, supported values are 'light', 'dark', 'no-preference'. See Page.EmulateMediaAsync(options) for more details. Defaults to 'light'.#

    • DeviceScaleFactor <double?> Specify device scale factor (can be thought of as dpr). Defaults to 1.#

    • Devtools <bool?> Chromium-only Whether to auto-open a Developer Tools panel for each tab. If this option is true, the headless option will be set false.#

    • DownloadsPath <string?> If specified, accepted downloads are downloaded into this directory. Otherwise, temporary directory is created and is deleted when browser is closed. In either case, the downloads are deleted when the browser context they were created in is closed.#

    • Env <IDictionary?<string, string>> Specify environment variables that will be visible to the browser. Defaults to process.env.#

    • ExecutablePath <string?> Path to a browser executable to run instead of the bundled one. If executablePath is a relative path, then it is resolved relative to the current working directory. Note that Playwright only works with the bundled Chromium, Firefox or WebKit, use at your own risk.#

    • ExtraHTTPHeaders <IDictionary?<string, string>> An object containing additional HTTP headers to be sent with every request. All header values must be strings.#

    • ForcedColors <enum ForcedColors { Active, None }?> Emulates 'forced-colors' media feature, supported values are 'active', 'none'. See Page.EmulateMediaAsync(options) for more details. Defaults to 'none'.#

      note

      It's not supported in WebKit, see here in their issue tracker.

    • Geolocation <Geolocation?>#

      • Latitude <double> Latitude between -90 and 90.
      • Longitude <double> Longitude between -180 and 180.
      • Accuracy <double?> Non-negative accuracy value. Defaults to 0.
    • HandleSIGHUP <bool?> Close the browser process on SIGHUP. Defaults to true.#

    • HandleSIGINT <bool?> Close the browser process on Ctrl-C. Defaults to true.#

    • HandleSIGTERM <bool?> Close the browser process on SIGTERM. Defaults to true.#

    • HasTouch <bool?> Specifies if viewport supports touch events. Defaults to false.#

    • Headless <bool?> Whether to run browser in headless mode. More details for Chromium and Firefox. Defaults to true unless the devtools option is true.#

    • HttpCredentials <HttpCredentials?> Credentials for HTTP authentication.#

    • IgnoreAllDefaultArgs <bool?> If true, Playwright does not pass its own configurations args and only uses the ones from args. Dangerous option; use with care. Defaults to false.#

    • IgnoreDefaultArgs <IEnumerable?<string>> If true, Playwright does not pass its own configurations args and only uses the ones from args. Dangerous option; use with care.#

    • IgnoreHTTPSErrors <bool?> Whether to ignore HTTPS errors during navigation. Defaults to false.#

    • IsMobile <bool?> Whether the meta viewport tag is taken into account and touch events are enabled. Defaults to false. Not supported in Firefox.#

    • JavaScriptEnabled <bool?> Whether or not to enable JavaScript in the context. Defaults to true.#

    • Locale <string?> Specify user locale, for example en-GB, de-DE, etc. Locale will affect navigator.language value, Accept-Language request header value as well as number and date formatting rules.#

    • Offline <bool?> Whether to emulate network being offline. Defaults to false.#

    • Permissions <IEnumerable?<string>> A list of permissions to grant to all pages in this context. See BrowserContext.GrantPermissionsAsync(permissions, options) for more details.#

    • Proxy <Proxy?> Network proxy settings.#

      • Server <string> Proxy to be used for all requests. HTTP and SOCKS proxies are supported, for example http://myproxy.com:3128 or socks5://myproxy.com:3128. Short form myproxy.com:3128 is considered an HTTP proxy.
      • Bypass <string?> Optional coma-separated domains to bypass proxy, for example ".com, chromium.org, .domain.com".
      • Username <string?> Optional username to use if HTTP proxy requires authentication.
      • Password <string?> Optional password to use if HTTP proxy requires authentication.
    • RecordHarOmitContent <bool?> Optional setting to control whether to omit request content from the HAR. Defaults to false.#

    • RecordHarPath <string?> Enables HAR recording for all pages into the specified HAR file on the filesystem. If not specified, the HAR is not recorded. Make sure to call BrowserContext.CloseAsync() for the HAR to be saved.#

    • RecordVideoDir <string?> Enables video recording for all pages into the specified directory. If not specified videos are not recorded. Make sure to call BrowserContext.CloseAsync() for videos to be saved.#

    • RecordVideoSize <RecordVideoSize?> Dimensions of the recorded videos. If not specified the size will be equal to viewport scaled down to fit into 800x800. If viewport is not configured explicitly the video size defaults to 800x450. Actual picture of each page will be scaled down if necessary to fit the specified size.#

      • Width <int> Video frame width.
      • Height <int> Video frame height.
    • ReducedMotion <enum ReducedMotion { Reduce, NoPreference }?> Emulates 'prefers-reduced-motion' media feature, supported values are 'reduce', 'no-preference'. See Page.EmulateMediaAsync(options) for more details. Defaults to 'no-preference'.#

    • ScreenSize <ScreenSize?> Emulates consistent window screen size available inside web page via window.screen. Is only used when the viewport is set.#

      • Width <int> page width in pixels.
      • Height <int> page height in pixels.
    • SlowMo <double?> Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on.#

    • StrictSelectors <bool?> It specified, enables strict selectors mode for this context. In the strict selectors mode all operations on selectors that imply single target DOM element will throw when more than one element matches the selector. See Locator to learn more about the strict mode.#

    • Timeout <double?> Maximum time in milliseconds to wait for the browser instance to start. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.#

    • TimezoneId <string?> Changes the timezone of the context. See ICU's metaZones.txt for a list of supported timezone IDs.#

    • TracesDir <string?> If specified, traces are saved into this directory.#

    • UserAgent <string?> Specific user agent to use in this context.#

    • ViewportSize <ViewportSize?> Emulates consistent viewport for each page. Defaults to an 1280x720 viewport. Use ViewportSize.NoViewport to disable the default viewport.#

      • Width <int> page width in pixels.
      • Height <int> page height in pixels.
  • returns: <BrowserContext>#

Returns the persistent browser context instance.

Launches browser that uses persistent storage located at userDataDir and returns the only context. Closing this context will automatically close the browser.

BrowserType.Name#

Returns browser name. For example: 'chromium', 'webkit' or 'firefox'.