Playwright for .NET Proxy Setup
Microsoft Playwright for .NET brings the full power of Playwright's browser automation to C# and .NET applications. It supports proxy configuration at the browser launch level, routing all browser traffic through Hex Proxies for geo-targeted testing, web scraping, and automated data collection. The .NET API mirrors the Node.js Playwright API closely, with C# idioms for async/await and strong typing.
Complete Configuration with Authentication
var proxyUser = Environment.GetEnvironmentVariable("PROXY_USER"); var proxyPass = Environment.GetEnvironmentVariable("PROXY_PASS");
using var playwright = await Playwright.CreateAsync(); await using var browser = await playwright.Chromium.LaunchAsync( new BrowserTypeLaunchOptions { Proxy = new Proxy { Server = "http://gate.hexproxies.com:8080", Username = proxyUser, Password = proxyPass }, Headless = true });
var context = await browser.NewContextAsync( new BrowserNewContextOptions { UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)", ViewportSize = new ViewportSize { Width = 1920, Height = 1080 }, Locale = "en-US", TimezoneId = "America/New_York" });
var page = await context.NewPageAsync(); page.SetDefaultTimeout(60000);
await page.GotoAsync("https://httpbin.org/ip"); var content = await page.TextContentAsync("body"); Console.WriteLine(content); ```
Geo-Targeted Browser Automation
Route traffic through specific countries by appending the country code to the proxy username:
await using var browser = await playwright.Chromium.LaunchAsync(
new BrowserTypeLaunchOptions
{
Proxy = new Proxy
{
Server = "http://gate.hexproxies.com:8080",
Username = proxyUser + "-country-de",
Password = proxyPass
}// Browser context with matching German locale var context = await browser.NewContextAsync( new BrowserNewContextOptions { Locale = "de-DE", TimezoneId = "Europe/Berlin" }); ```
Multi-Region Testing Pattern
var regions = new[] {
("us", "en-US", "America/New_York"),
("gb", "en-GB", "Europe/London"),
("de", "de-DE", "Europe/Berlin")foreach (var (country, locale, tz) in regions) { await using var browser = await playwright.Chromium.LaunchAsync( new BrowserTypeLaunchOptions { Proxy = new Proxy { Server = "http://gate.hexproxies.com:8080", Username = $"{proxyUser}-country-{country}", Password = proxyPass } });
var ctx = await browser.NewContextAsync( new BrowserNewContextOptions { Locale = locale, TimezoneId = tz }); var page = await ctx.NewPageAsync(); await page.GotoAsync("https://example.com/pricing");
var price = await page.TextContentAsync(".price"); Console.WriteLine($"[{country}] Price: {price}"); } ```
Common Pitfalls
Set extended timeouts (60 seconds) for page navigation through residential proxies — the proxy adds 200-500ms latency per request, and JavaScript-heavy pages may need additional time. Match the browser context locale and timezone to the proxy's geographic region for consistent fingerprinting. Anti-bot systems check for mismatches between IP geolocation and browser locale settings.
Integration with NUnit/xUnit
Playwright for .NET integrates cleanly with .NET test frameworks. Create a base test class that configures the proxy browser, and inherit from it in your regional test classes.