Now it screenshots

This commit is contained in:
2023-04-20 10:01:02 +02:00
parent 4230696888
commit 1cfaaaecb1
2 changed files with 66 additions and 5 deletions

View File

@@ -29,11 +29,14 @@
private void InitializeComponent()
{
this.btnStart = new System.Windows.Forms.Button();
this.pbxResult = new System.Windows.Forms.PictureBox();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pbxResult)).BeginInit();
this.SuspendLayout();
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(300, 165);
this.btnStart.Location = new System.Drawing.Point(12, 12);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(171, 97);
this.btnStart.TabIndex = 0;
@@ -41,14 +44,36 @@
this.btnStart.UseVisualStyleBackColor = true;
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// pbxResult
//
this.pbxResult.Location = new System.Drawing.Point(189, 12);
this.pbxResult.Name = "pbxResult";
this.pbxResult.Size = new System.Drawing.Size(599, 426);
this.pbxResult.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.pbxResult.TabIndex = 1;
this.pbxResult.TabStop = false;
//
// button1
//
this.button1.Location = new System.Drawing.Point(12, 115);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(171, 97);
this.button1.TabIndex = 2;
this.button1.Text = "Stop";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.button1);
this.Controls.Add(this.pbxResult);
this.Controls.Add(this.btnStart);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.pbxResult)).EndInit();
this.ResumeLayout(false);
}
@@ -56,6 +81,8 @@
#endregion
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.PictureBox pbxResult;
private System.Windows.Forms.Button button1;
}
}

View File

@@ -22,6 +22,7 @@ namespace Selenium_Clean
public partial class Form1 : Form
{
public const string COOKIE_HOST = ".formula1.com";
CancellationTokenSource CancelScreenShots = new CancellationTokenSource();
public Form1()
{
InitializeComponent();
@@ -75,7 +76,24 @@ namespace Selenium_Clean
return value;
}
private void btnStart_Click(object sender, EventArgs e)
async Task screens(FirefoxDriver driver,CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
Screenshot scrsht = ((ITakesScreenshot)driver).GetScreenshot();
// Get the raw bytes of the screenshot
byte[] screenshotBytes = Convert.FromBase64String(scrsht.AsBase64EncodedString);
// Create a new MemoryStream from the bytes
MemoryStream stream = new MemoryStream(screenshotBytes);
// Create a new Bitmap object from the MemoryStream
Bitmap bitmap = new Bitmap(stream);
pbxResult.Image = bitmap;
Thread.Sleep(1000);
}
}
private async void btnStart_Click(object sender, EventArgs e)
{
string loginCookieName = "login";
string loginSessionCookieName = "login-session";
@@ -91,10 +109,14 @@ namespace Selenium_Clean
FirefoxOptions options = new FirefoxOptions();
options.AddArgument("--window-size="+windowWidth+","+windowHeight+"");
options.AddArgument("--disable-headless");
//options.AddArgument("--disable-headless");
options.AddArgument("--disable-web-security");
options.AddArgument("--same-site-none-secure");
options.AcceptInsecureCertificates = true;
//Headless part
options.AddArgument("--headless");
options.AddArgument("--silent-launch");
options.AddArgument("--no-startup-window");
using (var driver = new FirefoxDriver(service,options))
{
@@ -108,7 +130,7 @@ namespace Selenium_Clean
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.Navigate().GoToUrl("https://f1tv.formula1.com/detail/1000006436/2023-saudi-arabian-grand-prix?action=play");
//Waits for the page to fully load
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(30);
@@ -152,8 +174,20 @@ namespace Selenium_Clean
Thread.Sleep(5000);
//Now you can start taking screenshots
CancelScreenShots = new CancellationTokenSource();
CancellationToken cancellationToken = CancelScreenShots.Token;
// Start the loop in a separate thread
await Task.Run(async () =>
{
await screens(driver,cancellationToken);
}, cancellationToken);
}
}
private void button1_Click(object sender, EventArgs e)
{
CancelScreenShots.Cancel();
}
}
}