TakeScreenShot of WebElement

674 views
Skip to first unread message

SantoshSarma

unread,
Sep 8, 2015, 8:46:57 AM9/8/15
to Selenium Users
As WebDriver changelog shows, I've tried to take screenshot of webElement. It is throwing Exception.





Code

File scrFile = (File)(driver.findElement(By.tagName("body"))).getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(scrFile, new File("/Users/test/Desktop/santosh.png"));


Exception

org.openqa.selenium.UnsupportedCommandException: Unrecognized command: GET /session/08965f64-0664-7f4c-9706-7c6058d05215/screenshot/%7B0988bcf4-35aa-df4c-aa49-773d41e5a737%7D

Command duration or timeout: 5 milliseconds

Build info: version: '2.47.1', revision: '411b314', time: '2015-07-30 03:03:16'

System info: host: 'test', ip: '127.0.0.1', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.10.5', java.version: '1.7.0_60'

Session ID: 08965f64-0664-7f4c-9706-7c6058d05215

Driver info: org.openqa.selenium.firefox.FirefoxDriver

Capabilities [{platform=MAC, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, browserConnectionEnabled=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=28.0}]


        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

       at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)

       at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

       at java.lang.reflect.Constructor.newInstance(Constructor.java:526)

       at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)

       at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)

       at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595)

       at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:273)

       at org.openqa.selenium.remote.RemoteWebElement.getScreenshotAs(RemoteWebElement.java:371)



What is the problem in it? 

Thanks in advance.

Regards,
SantoshSarma J V


Krishnan Mahadevan

unread,
Sep 8, 2015, 8:58:34 AM9/8/15
to Selenium Users
Santosh,

Looks like the implementation to support taking of screenshots at the element level has been added only to the client bindings as part of this commit.
But do we know if the implementation has been added to the server components yet [Firefox plugin, IEDriverServer and ChromeDriver binary ] ? The error message seems to suggest that there is no implementation for the JSONWireProtocol end-point /session/:sessionId/screenshot/:id


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/6d5523e0-d14b-47e0-bb0e-739b77327d28%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David

unread,
Sep 8, 2015, 5:35:02 PM9/8/15
to Selenium Users
So I guess for now, probably best to take screenshot of whole page/viewport and then just crop down to WebElement using it's size & location attributes (cropping using one's native programming/scripting language image/graphics library capabilities).

SantoshSarma

unread,
Sep 9, 2015, 3:14:39 AM9/9/15
to Selenium Users
Thanks @Krishnan !

My problem is, 

If page has internal scroll (Not entire page scroll) how to take the full screenshot of the part which has internal scroll? (Now its taking only visible area / page level scrolling area but not internal scrolling parts.)

Is there any other solution for this ? 

Kaleem Uddin Mohammed Abdul

unread,
Sep 11, 2015, 1:02:15 AM9/11/15
to Selenium Users
+1 For David response.

The below code can be used in Java
 WebDriver driver=new FirefoxDriver();
           driver.navigate().to("https://incometaxindiaefiling.gov.in/e-Filing/UserLogin/LoginHome.html");
           driver.manage().window().maximize();
           TakesScreenshot screenshot=(TakesScreenshot)driver;
           byte[] arrScreen = screenshot.getScreenshotAs(OutputType.BYTES);
           BufferedImage imageScreen = ImageIO.read(new ByteArrayInputStream(arrScreen));
           WebElement cap = driver.findElement(By.id("captchaImg"));
           Dimension capDimension = cap.getSize();
           Point capLocation = cap.getLocation();
           BufferedImage imgCap = imageScreen.getSubimage(capLocation.x, capLocation.y, capDimension.width, capDimension.height);

           ByteArrayOutputStream os = new ByteArrayOutputStream();
           ImageIO.write(imgCap, "png", os);

The below code is for C#
            _WebDriver.Navigate().GoToUrl("https://incometaxindiaefiling.gov.in/e-Filing/UserLogin/LoginHome.html");
            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
            // Begin timing
            stopwatch.Start();
            bool isDocumentReady = false;
            IJavaScriptExecutor JSE = _WebDriver as IJavaScriptExecutor;
            // Try for 3 minutes
            while (!isDocumentReady && stopwatch.ElapsedMilliseconds < 120000)
            {
                isDocumentReady = (bool)JSE.ExecuteScript("return document.readyState == 'complete'");
                // wait for half a second before trying again
                System.Threading.Thread.Sleep(500);
            }
            if (!isDocumentReady) throw new TimeoutException();
            _WebDriver.Manage().Window.Maximize();
            ITakesScreenshot _Screenshot = _WebDriver as ITakesScreenshot;
            Screenshot _SS = _Screenshot.GetScreenshot();
            byte[] arrScreen = _SS.AsByteArray;
            Stream abc = new MemoryStream(arrScreen);
            System.Drawing.Image imageScreen = System.Drawing.Bitmap.FromStream(abc);



            IWebElement cap = _WebDriver.FindElement(By.Id("captchaImg"));
            Size capDimension = cap.Size;
            Point capLocation = cap.Location;
            System.Drawing.Bitmap temp = (Bitmap)imageScreen;
            Rectangle Rec = new Rectangle(capLocation, capDimension);
            Bitmap temp1 = temp.Clone(Rec, System.Drawing.Imaging.PixelFormat.DontCare);
            string FileName = _DataSerializer.GetFileNameWithoutExtension() + ".png";
            temp1.Save(FileName);

SantoshSarma

unread,
Sep 21, 2015, 7:49:41 AM9/21/15
to Selenium Users
Thanks David & Mohammed Abdul !

Consider below screenshot. If I takeScreenShot it is taking only visible area. If I want to take screenshot of all the replies of this thread, can we achieve that by some how? 



//Code
driver.get("https://goo.gl/rgXBbz");
//here I want to take screenShot of all replies of this thread.


Thanks & Regards,
SantoshSarma

Albrecht Hilker

unread,
Nov 16, 2015, 9:52:57 PM11/16/15
to Selenium Users

> Consider below screenshot. If I takeScreenShot it is taking only visible area. If I want to take screenshot of all the replies of this thread, can we achieve that by some how?

Hello

You don't know what a screenshot is!
As the name says, a screenshot is as if you would take a camera and shoot a foto from the monitor of your computer.

This foto will NEVER contain parts that are scrolled out of view.

And with a little logic you can come on your own to the conclusion that this is COMPLETELY impossible.

Open a large Facebook page and you will notice that Facebook is optimized to load only those images that are currently visible.
(The same applies for Google image search)
Also further text is loaded by Ajax when scrolling down.
Without scrolling neither further text nor further images get loaded from the server.
Modern browsers render only the visible part of the screen otherwise they would be extremely slow on large pages.

To capture a large website you would have to take a screenshot, scroll a page down, take another screenshot, scroll down, etc.
Then you would have to set these images together, but this will not result well, because there will be overlappings.

Albrecht Hilker

unread,
Nov 18, 2015, 4:55:42 AM11/18/15
to Selenium Users
If I understood correctly PhantomJS is made to take screenshots from entire webpages.
May be you give it a try.

SantoshSarma

unread,
Nov 23, 2015, 8:10:15 AM11/23/15
to Selenium Users
Hi Albrecht Hilker,

Thanks for your reply.

I'm talking about the page with is loaded fully but, it has some scroll. Consider stackoverflow.com site. Attached screenshot taken for stackoverflow.com.



This screenshot taken using below logic only

File scrFile = (File)(driver.findElement(By.tagName("body"))).getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(scrFile, new File("/Users/test/Desktop/santosh.png"));


but, it is not returning the same if it has some internal scroll as explained above in same thread. 

I've attached screenshot taken for google image search using above logic.



I hope, you understand now clearly what my question is about. 
Reply all
Reply to author
Forward
0 new messages