Skip to main content
Version: 1.12.0

Network

Playwright provides APIs to monitor and modify network traffic, both HTTP and HTTPS. Any requests that page does, including XHRs and fetch requests, can be tracked, modified and handled.


HTTP Authentication#

using var context = await Browser.NewContextAsync(new BrowserNewContextOptions
{
HttpCredentials = new HttpCredentials
{
Username = "bill",
Password = "pa55w0rd"
},
});
var page = await context.NewPageAsync();
await page.GotoAsync("https://example.com");

API reference#

HTTP Proxy#

You can configure pages to load over the HTTP(S) proxy or SOCKSv5. Proxy can be either set globally for the entire browser, or for each browser context individually.

You can optionally specify username and password for HTTP(S) proxy, you can also specify hosts to bypass proxy for.

Here is an example of a global proxy:

var proxy = new Proxy
{
Server = "http://myproxy.com:3128",
Username = "user",
Password = "pwd"
};
await using var browser = await BrowserType.LaunchAsync(new BrowserTypeLaunchOptions
{
Proxy = proxy
});

When specifying proxy for each context individually, you need to give Playwright a hint that proxy will be set. This is done via passing a non-empty proxy server to the browser itself. Here is an example of a context-specific proxy:

var proxy = new Proxy { Server = "per-context" };
await using var browser = await BrowserType.LaunchAsync(new BrowserTypeLaunchOptions
{
Proxy = proxy
});
using var context = await Browser.NewContextAsync(new BrowserNewContextOptions
{
Proxy = new Proxy { Server = "http://myproxy.com:3128" })
});

Network events#

You can monitor all the requests and responses:

using Microsoft.Playwright;
using System;
class Program
{
public static async Task Main()
{
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync();
var page = await browser.NewPageAsync();
page.Request += (_, request) => Console.WriteLine(">> " + request.Method + " " + request.Url);
page.Response += (_, response) => Console.WriteLine("<<" + response.Status + " " + response.Url);
await page.GotoAsync("https://example.com");
}
}

Or wait for a network response after the button click:

// Use a glob URL pattern
var waitForResponseTask = page.WaitForResponseAsync("**/api/fetch_data");
await page.ClickAsync("button#update");
var response = await waitForResponseTask;

Variations#

// Use a regular expression
var waitForResponseTask = page.WaitForResponseAsync(new Regex("\\.jpeg$"));
await page.ClickAsync("button#update");
var response = await waitForResponseTask;
// Use a predicate taking a Response object
var waitForResponseTask = page.WaitForResponseAsync(r => r.Url.Contains(token));
await page.ClickAsync("button#update");
var response = await waitForResponseTask;

API reference#


Handle requests#

You can mock API endpoints via handling the network quests in your Playwright script.

Variations#

await page.RouteAsync("**/api/fetch_data", async route => {
await route.FulfillAsync(status: 200, body: testData);
});
await page.GotoAsync("https://example.com");

API reference#


Modify requests#

// Delete header
await page.RouteAsync("**/*", async route => {
var headers = new Dictionary<string, string>(route.Request.Headers.ToDictionary(x => x.Key, x => x.Value));
headers.Remove("X-Secret");
await route.ContinueAsync(new RouteContinueOptions { Headers = headers });
});
// Continue requests as POST.
await page.RouteAsync("**/*", async route => await route.ContinueAsync(method: "POST"));

You can continue requests with modifications. Example above removes an HTTP header from the outgoing requests.

Abort requests#

await page.RouteAsync("**/*.{png,jpg,jpeg}", route => route.AbortAsync());
// Abort based on the request type
await page.RouteAsync("**/*", async route => {
if ("image".Equals(route.Request.ResourceType))
await route.AbortAsync();
else
await route.ContinueAsync();
});

API reference#


WebSockets#

Playwright supports WebSockets inspection out of the box. Every time WebSocket is created, event Page.WebSocket event is fired. This event contains the WebSocket instance for further web socket frames inspection:

page.WebSocket += (_, ws) =>
{
Console.WriteLine("WebSocket opened: " + ws.Url);
ws.FrameSent += (_, f) => Console.WriteLine(f.Text);
ws.FrameReceived += (_, f) => Console.WriteLine(f.Text);
ws.Close += (_, ws1) => Console.WriteLine("WebSocket closed");
};

API reference#