1
votes

I'm using Selenium Webdriver automated testing on Browserstack and when I take a screenshot on Firefox and IE, it takes the screenshot of the whole page. But as my page contains a very long list - the screenshot is too 'long' and because of that it's not a clear image and it's difficult to see if the page behaves the right way. I need it to do exact as the Chrome does: the screenshot of the viewport width and height only. Is it possible on Firefox and IE?

The code I use:

Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
ss.SaveAsFile(imageSave, System.Drawing.Imaging.ImageFormat.Png);
1
you can always take a ss using the Windows built in one and crop itSteve
You can use crop the screenshot and save to new fileAkash KC
Tnx guys. Do I have to crop it manually? It's a plenty of images - that's impossible. Is there a way to make it automatic? @Stevebuca
@buca yea, you can use Windows API to get the window's position and size, then you can just crop it using code based on the position/sizeSteve
@Steve tnx I will try it.buca

1 Answers

0
votes

Upgrade to Selenium 3 version.

In Selenium 2.* versions, complete web page is taken as screenshot.

From Selenium 3.* version, only visible portion of the web page is taken as screenshot.

So, just upgrading the selenium version, you can achieve the behaviour, and the same code should work.

From Selenium official docs:

Screenshot ss = ((ITakesScreenshot)webDriver).GetScreenshot();
string screenshot = ss.AsBase64EncodedString;
byte[] screenshotAsByteArray = ss.AsByteArray;
ss.SaveAsFile(activeDir + TestSuiteName + "//" + FileNanme + imageFormat, ImageFormat.Jpeg);

I validated this behaviour in Python with Selenium 3.0.2 in both Chrome & Firefox

from selenium import webdriver

driver = webdriver.Firefox()
driver.get('http://stackoverflow.com/users/2575259/naveen')
driver.get_screenshot_as_file('D:/Naveen/so/google.png') 
driver.quit()