using System; using System.Threading.Tasks; using Kameleo.LocalApiClient; using Kameleo.LocalApiClient.Model; using Microsoft.Playwright; // This is the port the Kameleo Engine is listening on. Default value is 5050, but can be overridden in appsettings.json file if (!int.TryParse(Environment.GetEnvironmentVariable("KAMELEO_PORT"), out var KameleoPort)) { KameleoPort = 5050; } var client = new KameleoLocalApiClient(new Uri($"http://localhost:{KameleoPort}")); await client.VerifyEngineReadyAsync(); // Search Chrome fingerprints var fingerprints = await client.Fingerprint.SearchFingerprintsAsync(deviceType: "desktop", browserProduct: "chrome"); // Create a new profile with recommended settings // for browser fingerprint protection var createProfileRequest = new CreateProfileRequest(fingerprints[0].Id) { Name = "connect with Playwright to Chrome example", }; var profile = await client.Profile.CreateProfileAsync(createProfileRequest); // Start the Kameleo profile and connect with Playwright through CDP var browserWsEndpoint = $"ws://localhost:{KameleoPort}/playwright/{profile.Id}"; var playwright = await Playwright.CreateAsync(); var browser = await playwright.Chromium.ConnectOverCDPAsync(browserWsEndpoint, new() { NoDefaults = true, Timeout = 90_000 }); // It is recommended to work on the default context. // NOTE: We DO NOT recommend using multiple browser contexts, as this might interfere // with Kameleo's browser fingerprint modification features. var context = browser.Contexts[0]; var page = await context.NewPageAsync(); // Use any Playwright command to drive the browser // and enjoy full protection from bot detection products await page.GotoAsync("https://wikipedia.org"); await page.ClickAsync("[name=search]"); await page.Keyboard.TypeAsync("Chameleon"); await page.Keyboard.PressAsync("Enter"); // Wait for 5 seconds await Task.Delay(5_000); // Stop the browser by stopping the Kameleo profile await client.Profile.StopProfileAsync(profile.Id);