2

I want to take a screenshot of a webpage using Selenium. I have notice that the action to take the screenshot require to open the web browser itself. tried to change webDriver.Navigate().GoToUrl("http://www.google.com"); with webDriver.Url = "http://www.google.com"; but no success, I even tried to leave it with no url and the browser opened with url of 'data', which now I understand that something else makes the browser to be open.

private void button1_Click(object sender, EventArgs e)
{
   var capabilitiesInternet = new 
       OpenQA.Selenium.Remote.DesiredCapabilities();
   capabilitiesInternet.
       SetCapability("ignoreProtectedModeSettings", true);
   IWebDriver webDriver = new ChromeDriver();
   webDriver.Navigate().GoToUrl("http://www.google.com");

   Screenshot screenshot = ((ITakesScreenshot)webDriver).GetScreenshot();
   screenshot.SaveAsFile("E:\\ScreenShot.png", 
       System.Drawing.Imaging.ImageFormat.Png);
   webDriver.Quit();
}
Brian
  • 5,069
  • 7
  • 37
  • 47
zb22
  • 3,126
  • 3
  • 19
  • 34

2 Answers2

1

No - you need to let the WebDriver request the page, otherwise how can it know what screenshot to produce?

If you're trying to avoid a real, 'slow' browser starting up and opening a window, you should either consider running that browser headlessly, as per:

How do I run Selenium in Xvfb?

Or check out the headless WebKit browser PhantomJS (or maybe SlimerJS), and using almost exactly the same WebDriver API as you have now, ask it to produce your screenshots 'in-memory':

Phantomjs - take screenshot of a web page

Just replace:

WebDriver webDriver = new ChromeDriver();

with:

WebDriver webDriver = new PhantomJSDriver();

(Obviously requires the application to be installed locally)

Edit: Just a note that the typical use-case for this is 'overnight' continuous-integration / continuous-testing when run from headless CI servers. However, it can be very easily added to other work-flows, e.g. for visual regression-testing, and simple one-off checks.

Community
  • 1
  • 1
Andrew Regan
  • 5,087
  • 6
  • 37
  • 73
  • No, not an option, as your Firefox is a 'real' browser for users. But if you follow the first link it shows how to set up your graphics environment so that the rendering can happen 'in-memory' and so no user will see. – Andrew Regan Feb 01 '16 at 23:24
  • To be honest I recommend the PhantomJS approach - it's a very useful tool to add to your armoury - though bear in mind it won't necessarily render 100% the same as FF would. – Andrew Regan Feb 01 '16 at 23:31
0

There's multiple ways to go about it:

  • There is chrome flag --no-startup-window which should open chrome without window. I tried it, and it didn't work, however, you can give it a go.
  • Use hacks - there is a way to start chrome without you ever seeing it, it works nicely. Just use --window-position=-9999,0, note that, TakeScreenShot() has some weird anomalies in my experience - it focuses the window, it runs out of memory randomly, etc. What I ended up doing is I wrote chrome extension which can take screenshots and return them back to selenium through JS.
Erti-Chris Eelmaa
  • 25,338
  • 6
  • 61
  • 78