using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Threading; using OpenQA.Selenium; using OpenQA.Selenium.Firefox; using OpenQA.Selenium.Remote; using System.Net.Http; using System.Data.SQLite; using System.IO; using System.Diagnostics; using System.Reflection; namespace TestSelenium { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnStart_Click(object sender, EventArgs e) { string host = ".formula1.com"; string loginCookieName = "login"; string loginCookieValue = GetCookieValue(host, loginCookieName); string loginSessionName = "login-session"; string loginSessionValue = GetCookieValue(host, loginSessionName); FirefoxOptions options = new FirefoxOptions(); options.AddArgument("--disable-headless"); //options.AddArgument("headless"); //options.AddArgument("--silent-launch"); //options.AddArgument("--no-startup-window"); //FirefoxOptions options = ffopt.SetHeadless(true); //options.AddArgument("--headless"); options.AddArgument("--window-size=1920,1080"); options.AddArgument("--disable-web-security"); options.AddArgument("--same-site-none-secure"); options.AcceptInsecureCertificates = true; var driver = new FirefoxDriver(options); var loginCookie = new Cookie(loginCookieName, loginCookieValue , host, "/", DateTime.Now.AddDays(5)); var loginSessionCookie = new Cookie(loginSessionName, loginSessionValue, host, "/", DateTime.Now.AddDays(5)); driver.Navigate().GoToUrl("https://f1tv.formula1.com/"); driver.Manage().Cookies.AddCookie(loginCookie); driver.Manage().Cookies.AddCookie(loginSessionCookie); driver.Navigate().GoToUrl("https://f1tv.formula1.com/detail/1000005104/2022-bahrain-grand-prix?action=play"); driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(30); IWebElement conscentButton = driver.FindElement(By.Id("truste-consent-button")); conscentButton.Click(); driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(30); Thread.Sleep(5000); //IWebElement dataChannelButton = driver.FindElement(By.ClassName("btn btn-link d-flex align-items-center data-button")); IWebElement dataChannelButton = driver.FindElement(By.ClassName("data-button")); dataChannelButton.Click(); IWebElement fullScreenButton = driver.FindElement(By.ClassName("bmpui-ui-fullscreentogglebutton")); fullScreenButton.Click(); //MessageBox.Show("Clicked"); Thread.Sleep(5000); int counter = 0; while (true) { string filename = "C:\\Users\\Moi\\Pictures\\SeleniumScreens\\screen_" + counter + ".png"; ((ITakesScreenshot)driver).GetScreenshot().SaveAsFile(filename, ScreenshotImageFormat.Png); SendScreenshotToBroker(filename); counter++; Thread.Sleep(1000); } } private async void SendScreenshotToBroker(string filename) { var client = new HttpClient(); var message = new HttpRequestMessage { Method = HttpMethod.Post, RequestUri = new Uri("http://localhost:8888/"), Content = new StringContent("[ScreenPath] " + filename, Encoding.UTF8, "text/plain") }; var response = await client.SendAsync(message); var content = await response.Content.ReadAsStringAsync(); MessageBox.Show(content); } //Cookie Retrieval system public void RunCookieRetrieval() { string scriptPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "recoverCookiesCSV.py"); Process process = new Process(); process.StartInfo.FileName = "python.exe"; process.StartInfo.Arguments = scriptPath; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.Start(); string output = process.StandardOutput.ReadToEnd(); process.WaitForExit(); //MessageBox.Show("Python script finished with exit code " + process.ExitCode); } public string GetCookieValue(string host, string name) { RunCookieRetrieval(); string value = ""; List cookies = new List(); using (var reader = new StreamReader("cookies.csv")) { // Read the header row and validate column order string header = reader.ReadLine(); string[] expectedColumns = { "host_key", "name", "value", "path", "expires_utc", "is_secure", "is_httponly" }; string[] actualColumns = header.Split(','); for (int i = 0; i < expectedColumns.Length; i++) { if (expectedColumns[i] != actualColumns[i]) { throw new InvalidOperationException($"Expected column '{expectedColumns[i]}' at index {i} but found '{actualColumns[i]}'"); } } // Read each data row and parse values into a Cookie object while (!reader.EndOfStream) { string line = reader.ReadLine(); string[] fields = line.Split(','); string hostname = fields[0]; string cookieName = fields[1]; if (hostname == host && cookieName == name) { value = fields[2]; } } } return value; } } }