i am currently trying to develop a small c# script to screenshot a website.
I`m working with Visual Studio 2019 and my code is the following:
var service = ChromeDriverService.CreateDefaultService();
ChromeOptions option = new ChromeOptions();
option.AddArgument("--user-agent=Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.85 Safari/537.36");
service.HideCommandPromptWindow = false;
//option.AddArgument("--headless");
ChromeDriver driver = new ChromeDriver(service, option);
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(60);
driver.Navigate().GoToUrl("https://www.google.com");
OpenQA.Selenium.ITakesScreenshot screenshotDriver = driver as OpenQA.Selenium.ITakesScreenshot;
OpenQA.Selenium.Screenshot screenshot = screenshotDriver.GetScreenshot();
screenshot.SaveAsFile("C:/", OpenQA.Selenium.ScreenshotImageFormat.Png);
Now i get this error message:
System.UnauthorizedAccessException: "Access to the path "C:/" was denied.
I experimented with many different folders and it doesn't work with any of them. I tried opening Visual Studio as an administrator, but this did not solve the problem.
I guess it has something to do with missing write permissions for Visual Studio.
How can I fix this problem?
You are getting System.UnauthorizedAccessException because ,trying to overwrite your Screenshots directory with a .png file. you can use this method
public static string GetImageDirectory()
{
string cur = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string imageDir = Path.Combine(cur, @"..\..\", "Images");
SetAccessRule(imageDir);
return imageDir;
}
then
screenshot.SaveAsFile(GetImageDirectory(), ScreenshotImageFormat.Png);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.