Issue
I have the following program that I run every month on some website pages (you know Selenium testing etc.) and then I upload the output screenshots somewhere.
For some reason, suddenly this time the program threw this error and prompted me to run the last successful build from previous month. I chose that and got the screenshots as I wanted but I don't want to use last successful build every time, why is this error being thrown suddenly now when no code changes have been done?
Error CS0103 The name 'ScreenshotImageFormat' does not exist in the current context
program.cs
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System.IO;
using System.Configuration;
namespace SSL_Labs
{
class Program
{
static void Main(string[] args)
{
string[] domains;
// TODO: Config
domains = new string[] { "website1.com", "website2.com"};
foreach (string d in domains) {
GenerateSslLabsReport(d);
}
}
private static void GenerateSslLabsReport(string domain)
{
IWebDriver driver;
IWebElement hostName;
IWebElement hideResults;
IWebElement submit;
WebDriverWait waitForReport;
ITakesScreenshot screenshot;
string outputBasePath = "C:\\Users\\User\\Documents\\SSL\\";
string timestamp = DateTime.Now.ToString("yyyy-MM");
string subDirectory = "Output_" + timestamp;
string outputPath = Path.Combine(outputBasePath, subDirectory) + "\\";
Directory.CreateDirectory(outputPath);
string createdDirectoryPath = outputPath;
/* Uncomment below code if you want to test directory path is correct
string fileName = "sample.txt";
string filePath = Path.Combine(createdDirectoryPath, fileName);
File.WriteAllText(filePath, "This is a sample text file.");
if (File.Exists(filePath))
{
Console.WriteLine("File created successfully.");
}
else
{
Console.WriteLine("File not created.");
}
*/
Console.WriteLine("Starting SSL Labs reporting ...");
driver = null;
try
{
driver = new ChromeDriver();
driver.Manage().Window.Maximize();
driver.Url = "https://www.ssllabs.com/ssltest/";
hideResults = driver.FindElement(By.XPath(".//*[@id='hideResults']"));
hideResults.Click();
hostName = driver.FindElement(By.XPath(".//*[@name='d']"));
hostName.SendKeys(domain);
submit = driver.FindElement(By.XPath(".//*[@type='submit']"));
submit.Click();
waitForReport = new WebDriverWait(driver, new TimeSpan(0, 0, 120));
waitForReport.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath("//*[@id='rating']")));
// TODO: Config
File.WriteAllText(string.Format(createdDirectoryPath + "{0}.html", domain), driver.PageSource.Replace("<head>", "<head><base href=\"https://www.ssllabs.com/\" target=\"_blank\"></base"));
screenshot = (ITakesScreenshot)driver;
screenshot.GetScreenshot().SaveAsFile(string.Format(createdDirectoryPath + "{0}.png", domain), ScreenshotImageFormat.Png);
driver.Close();
}
catch (Exception e)
{
Console.WriteLine($"Error processing domain: {domain}");
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
finally
{
if (driver != null) driver.Dispose();
}
}
}
}
The only thing I did was update the Selenium drivers as I usually do every month, so that it doesn't conflict with the new Chrome updates.
Solution
Try not to pass ScreenshotImageFormat.Png
I mean
screenshot.GetScreenshot().SaveAsFile(string.Format(createdDirectoryPath + "{0}.png", domain));
Answered By - silentwarrior
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.