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

空扰寡人 提交于 2021-02-07 06:25:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!