Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Taking an IE screenshot returns a black image

Tags:

I'm building a console app which will connect to different computers in the network and take browser screenshots of a webpage.
Using Selenium 2.47.1 to set up server & nodes. The console app runs in the PC which is set up as selenium hub.
The screenshot is fine in firefox,chrome,ie from the hub computer.
The screenshot is also fine in firefox in remote pc.
But with IE it returns a black image.
Both the hub and node computers run on windows 7 64-bit, have IE11. I am using the 64bit IEDriver in both PCs. The node computer is not locked.

using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;

static void Main(string[] args)
 {
   IWebDriver NewDriver = null;
   using (NewDriver = new RemoteWebDriver(new Uri("http://172.165.10.111/wd/hub"), DesiredCapabilities.InternetExplorer()))
    {
      if (NewDriver != null)
       {
         NewDriver.Navigate().GoToUrl("http://www.google.com");
         NewDriver.Manage().Window.Size = new Size(1804, 1096);
         Screenshot ss = ((ITakesScreenshot)NewDriver).GetScreenshot();
         ICapabilities capabilities = ((RemoteWebDriver)NewDriver).Capabilities;
         ss.SaveAsFile(@"C:\Path\123.png", ImageFormat.Png);
         NewDriver.Quit();
       }
    }
 }
like image 941
Qwerty Avatar asked Aug 27 '15 06:08

Qwerty


People also ask

How come when I screenshot A picture is black?

This can be caused by apps that turn the screen black to prevent capturing such as video streaming apps, games, overlays, or anti-virus software. It may also be caused by using Windows without activating it.

How do you take a screenshot on Internet Explorer?

Press Ctrl + PrtScn keys. The entire screen changes to gray including the open menu. Select Mode, or in earlier versions of Windows, select the arrow next to the New button. Select the kind of snip you want, and then select the area of the screen capture that you want to capture.


2 Answers

It's because your screen is locked, or a screensaver is running.

You will need to turn off your screensaver and configure windows to never lock itself when not in use. To turn off your screensaver:

  • Click the Start button.
  • Click Control Panel.
  • In the search box, type screen saver.
  • Click Turn screen saver on or off.

Then modify your screensaver settings. Make sure you have unchecked "On resume, display logon screen".

While you're in the control panel it's probably worth checking the power options and making sure the machine isn't going to sleep or powering down after a set period of time as well.

You will also want to use VNC or remote assist to access the GUI. If you RDP in it will lock the screen for the local user who is currently logged in and again it will lock the screen when you disconnect.

Finally don't use the 64-bit IE driver, you should be using the 32Bit one. Nobody runs the 64Bit version of IE (even if they have a 64Bit capable machine).

****** Edit Adding a bit more info from a credible and reputable source ******

Below is a link to a post on the Selenium users forum where Jim Evans (the developer who wrote the IEDriver binaries) explains this:

http://selenium.10932.n7.nabble.com/IE-Screenshots-are-black-when-not-connected-via-Remote-Desktop-to-the-VM-hosting-an-IE-Node-td37004.html

This quote in particular about taking screenshots when you have disconnected from a RDP instance:

It's a known limitation. There is no known workaround. Complain to Microsoft. They're the ones that make the PrintWindow API (which is the proper API to use when grabbing screen captures) behave that way. Either that, or if you discover a way to make it work, you're welcome to submit a patch.

He explains how the screenshot code works in more detail on his blog here:

http://jimevansmusic.blogspot.co.uk/2014/09/screenshots-sendkeys-and-sixty-four.html

Specifically:

The IE driver takes screenshots using the PrintWindow API function. PrintWindow can only take a screenshot of the visible portion of any given window, which means that in order to get a full-page screenshot (as required by the WebDriver API), the window must be sized large enough to display the entire page without scroll bars. However, Windows does not allow the window to be resized larger than the visible screen resolution. When we ask IE to resize itself, a WM_GETMINMAXINFO message is sent on a resize event so the IE can figure how large a window can be. By intercepting that message with a hook, and modifying the max values, we can trick IE into thinking that a window can be sized greater than the screen resolution would otherwise allow.

like image 102
Ardesco Avatar answered Oct 05 '22 18:10

Ardesco


It looks like this might be a known issue with configuration:

https://code.google.com/p/selenium/issues/detail?id=3536

From what @ShubhasmitGupta mentioned above, it does look like the IE driver hands out black-screen screenshots when the desktop is locked (I'm assuming this has something to do with DWM/Explorer not rendering windows).

There's a workaround involving not locking the targeted computer when connecting to it via RDP. Essentially, you create a disconnect.bat file with the following contents:

tscon rdp-tcp#0 /dest:console

I'm not sure exactly when that's supposed to be run, but the concept is to prevent the test computer from being locked when connecting via Remote Desktop. If that helps, great; I haven't been able to test the workaround myself and figure out how to set it up. Maybe someone else can write a more concise answer.

like image 32
Shotgun Ninja Avatar answered Oct 05 '22 19:10

Shotgun Ninja