using Kameleo.LocalApiClient; using Kameleo.LocalApiClient.Model; using OpenQA.Selenium.Chrome; using OpenQA.Selenium.Remote; using System; using System.IO; using System.Threading.Tasks; // 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(browserProduct: "chrome"); // Create a new profile with recommended settings // for browser fingerprint protection var createProfileRequest = new CreateProfileRequest(fingerprints[0].Id) { Name = "file upload example", }; var profile = await client.Profile.CreateProfileAsync(createProfileRequest); // Start the Kameleo profile and connect using WebDriver protocol var uri = new Uri($"http://localhost:{KameleoPort}/webdriver"); var opts = new ChromeOptions(); opts.AddAdditionalOption("kameleo:profileId", profile.Id.ToString()); var driver = new RemoteWebDriver(uri, opts); driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(15); // Open uplad site driver.Url = "https://the-internet.herokuapp.com/upload"; await Task.Delay(3_000); // Upload file var filePath = Path.Combine(Environment.CurrentDirectory, "kameleo.png"); var fileInput = driver.FindElementByCssSelector("input[type=file]"); fileInput.SendKeys(filePath); driver.FindElementById("file-submit").Click(); // Check file upload success var fileNameElement = driver.FindElementById("uploaded-files"); Console.WriteLine("uploaded file name: " + fileNameElement.Text); await Task.Delay(5_000); // Stop the browser by stopping the Kameleo profile await client.Profile.StopProfileAsync(profile.Id);