问题
I am using Selenium 2 (Webdriver), in an ASP.NET website to build a service, where users can enter their URL and gets screenshots of the page, made with different browsers.
My page is hostet on an Windows Server 2008 R2.
Taking Screenshots with FirefoxDriver
works perfect.
But when I am using InternetExplorerDriver
, I just get an empty black file.
The App is running as Administrator - so there should't be permission issues.
My Code:
// Opening the Browser
var ieCapabilities = DesiredCapabilities.InternetExplorer();
ieCapabilities.SetCapability(InternetExplorerDriver.IntroduceInstabilityByIgnoringProtectedModeSettings, true);
var browserIe = new InternetExplorerDriver(ieCapabilities);
browserIe.Navigate().GoToUrl("http://www.google.com");
// Screenshot
var dir = Server.MapPath("/screenshots/");
browserIe.GetScreenshot().SaveAsFile(dir + "Filename.png", ImageFormat.Png);
browserIe.Close();
Any ideas why my file is black? THANKS!
回答1:
There's probably nothing wrong with your code. Although, I'm using Java, so I can't tell for sure.
I had the same issue with IE while FF and Chrome worked fine.
This post suggests that starting the Selenium Server via a remote desktop connection could lead to problems.
Some other posts suggest that the screen saver might have something to do with it.
I just tried leaving the remote desktop connection open and it solved the black screenshot issue. Also logging in via VNC seems to work, leading me to the theory that Windows locks the screen after terminating the remote desktop connection while leaving it unlocked if using VNC.
This post suggests that disabling screenshots when the screen is locked is a Windows Security feature.
回答2:
InternetExplorerDriver mydriver = new InternetExplorerDriver();
mydriver.Navigate().GoToUrl("http://www.google.com/");
Screenshot myScrennShot = ((ITakesScreenshot)iedriver).GetScreenshot();
myScrennShot.SaveAsFile(@"C:\Path\123.png", ImageFormat.Png);
//or
byte[] data = myScrennShot.AsByteArray;
It works for me, probably it does work for you too :-) If it doesn't work I suggest you to separate this code to different service (WindowsService) because in you case this issue maybe connected with application pool restrictions. Anyways, please let me know how is it going.
来源:https://stackoverflow.com/questions/8963045/selenium-2-webdriver-taking-a-screenshot-returns-a-black-image