我每个月都会在一些网站页面上运行下面的程序(你知道的,selenium测试等等),然后我会把输出的截图上传到某个地方.
由于某种原因,这一次突然程序抛出了这个错误,并提示我运行上个月的最后一个成功构建.我 Select 了它,并获得了我想要的屏幕截图,但我不想每次都使用上次成功的构建,为什么在没有完成代码更改的情况下,现在突然抛出这个错误?
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();
}
}
}
}
我所做的唯一一件事就是像我通常每个月做的那样更新Selify驱动程序,这样它就不会与新的Chrome更新冲突.