Interface IBrowserType
- Namespace
- Microsoft.Playwright
- Assembly
- Microsoft.Playwright.dll
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();
}
}
public interface IBrowserType
Properties
ExecutablePath
A path where Playwright expects to find a bundled browser executable.
string ExecutablePath { get; }
Property Value
Name
Returns browser name. For example: 'chromium'
, 'webkit'
or 'firefox'
.
string Name { get; }
Property Value
Methods
ConnectAsync(string, BrowserTypeConnectOptions?)
This method attaches Playwright to an existing browser instance. When connecting
to another browser launched via BrowserType.launchServer
in Node.js, the
major and minor version needs to match the client version (1.2.3 → is compatible
with 1.2.x).
Task<IBrowser> ConnectAsync(string wsEndpoint, BrowserTypeConnectOptions? options = null)
Parameters
wsEndpoint
stringA browser websocket endpoint to connect to.
options
BrowserTypeConnectOptionsCall options
Returns
ConnectOverCDPAsync(string, BrowserTypeConnectOverCDPOptions?)
This method attaches Playwright to an existing browser instance using the Chrome DevTools Protocol.
The default browser context is accessible via Contexts.
**Usage**
var browser = await playwright.Chromium.ConnectOverCDPAsync("http://localhost:9222");
var defaultContext = browser.Contexts[0];
var page = defaultContext.Pages[0];
Task<IBrowser> ConnectOverCDPAsync(string endpointURL, BrowserTypeConnectOverCDPOptions? options = null)
Parameters
endpointURL
stringA CDP websocket endpoint or http url to connect to. For example
http://localhost:9222/
orws://127.0.0.1:9222/devtools/browser/387adf4c-243f-4051-a181-46798f4a46f4
.options
BrowserTypeConnectOverCDPOptionsCall options
Returns
Remarks
Connecting over the Chrome DevTools Protocol is only supported for Chromium-based browsers.
LaunchAsync(BrowserTypeLaunchOptions?)
Returns the browser instance.
**Usage**
You can use ignoreDefaultArgs
to filter out --mute-audio
from default arguments:
var browser = await playwright.Chromium.LaunchAsync(new() {
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.
Task<IBrowser> LaunchAsync(BrowserTypeLaunchOptions? options = null)
Parameters
options
BrowserTypeLaunchOptionsCall options
Returns
LaunchPersistentContextAsync(string, BrowserTypeLaunchPersistentContextOptions?)
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.
Task<IBrowserContext> LaunchPersistentContextAsync(string userDataDir, BrowserTypeLaunchPersistentContextOptions? options = null)
Parameters
userDataDir
stringPath 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
. Pass an empty string to use a temporary directory instead.options
BrowserTypeLaunchPersistentContextOptionsCall options