Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium 2 (webdriver): Taking a Screenshot returns a black image

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!

like image 567
Gerwald Avatar asked Jan 22 '12 17:01

Gerwald


People also ask

Can we take screenshot in Selenium WebDriver?

To capture screenshots in Selenium, one has to utilize the method TakesScreenshot. This notifies WebDriver that it should take a screenshot in Selenium and store it. OutputType defines the output type for the required screenshot in the above snippet.

What is the code for screenshots using Selenium WebDriver?

To capture a screenshot in Selenium, we can make use of an interface, called TakesScreenshot. This method indicates the driver, that it can capture a screenshot and store it in different ways. File file = ((TakesScreenshot) driver). getScreenshotAs(OutputType.

How do you take a screenshot on Webelement?

Just pass the Webelement name and your file name in your generic “elementScreenshot” method. This will take three screenshots together. For the Target file in which you want to copy your source file, you can also give any path to store your screenshot.

Which of the following image formats can be used to save the captured screenshot in Selenium?

You can provide the file type as jpg, png, etc. ImageIO. write(screenshot.


1 Answers

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.

like image 63
mwuertinger Avatar answered Sep 21 '22 18:09

mwuertinger