How to capture screenshot only the specific element? Selenium WebDriver

9,233 views
Skip to first unread message

Jaydenz

unread,
Dec 11, 2012, 10:34:31 PM12/11/12
to seleniu...@googlegroups.com

Currently I trying to capture a screenshot using the Selenium WebDriver but most far as i can go is just the whole page screen shot. However, what I wanted is just to capture a part of the page or perhaps just on specific element based on id. Any way to capture a screenshot by selected item or element?

For example, i wish to capture the picture with image id = "Butterfly" only... Is it possible to do that? Thank in advance.

Manoj Hans

unread,
Dec 13, 2012, 1:00:07 AM12/13/12
to seleniu...@googlegroups.com
Hi Jaydenz,

It is possible to take screenshot of specific element....see this post it will help u out




--Manoj Hans

Jaydenz

unread,
Dec 19, 2012, 4:19:07 AM12/19/12
to seleniu...@googlegroups.com
Hi Manoj, thank you for reply. Anyway i was able to screen capture but i get stuck because i could not crop the correct position of the image that i want.
Actually, the image i want to screen capture will keep changing for every run. So if i just download it directly, it will give me another image, while it is different with the one i want at the website page already.
So now, i am trying to crop the image. However, i could not get the right position for it. Could you help me out with this? 
IWebElement img = driver.FindElement(By.Id("IMG1"));
int width = img.Size.Width;
int height = img.Size.Height;
Point point = img.Location;
int x = point.Location.X;;
int y = point.Location.Y;
RectangleF part = new RectangleF(x, y, width, height);
Bitmap bmpobj = new Bitmap(filePath);
Bitmap bn = bmpobj.Clone(part, bmpobj.PixelFormat);


PS: The website i tried to screen capture contain an iframe content which load a long page with scroll bar. Is that possible the location of the image is far too down causing me unable to get the right coordinate after all?

David

unread,
Nov 22, 2013, 7:13:57 PM11/22/13
to seleniu...@googlegroups.com
Jaydenz, did you get any updates since your old post here? Did you solve the problem yourself?

Probably the best debugging for the problem you reported would have been to evaluate what the cropped image looked like and also if you got any exceptions or things ran fine except that the cropped image is incorrect.

Sravan Kumar

unread,
Apr 4, 2014, 3:00:50 PM4/4/14
to seleniu...@googlegroups.com
Hi ,

It is possible to take the screen shot of the specific element using Selenium

Procedure to follow:

Take the total page screen shot
 Get the Width and Height of the WebElement using
int width = element.getSize().getWidth();
int height = element.getSize().getHeight();
Create a rectangle using Width and Height
      Rectangle rect = new Rectangle(width, height);
Get the Location of WebElement in a Point.This will provide X & Y co-ordinates of the WebElement
Point p = element.getLocation();

Create image by for element using its location and size.Write back the image data for element in File object

Hear is the sample code snippet for that





On Wednesday, December 12, 2012 9:04:31 AM UTC+5:30, Jaydenz wrote:

Anirudha Chavan

unread,
May 27, 2015, 4:34:14 PM5/27/15
to seleniu...@googlegroups.com
Hey Sravan thanks for this solution. This solution is working fine with pages designed in JSF, for pages which are designed in angular this is not giving image of actual element. Image returned is different than actual element.
Can you please tell me what should I use to take screenshot for angular pages and then crop them as you mentioned above. Thanks in advance.

David

unread,
May 28, 2015, 4:56:11 PM5/28/15
to seleniu...@googlegroups.com
what image do you get back instead of the expected one? how far off in location is it to the expected image?

Anirudha Chavan

unread,
May 29, 2015, 3:52:30 AM5/29/15
to seleniu...@googlegroups.com
Hi David,
I get back images of header and difference in location is too far its like image I am getting is from top left corner and I want image of right left corner.

Nikesh Jauhari

unread,
May 29, 2015, 1:31:01 PM5/29/15
to seleniu...@googlegroups.com
      File screen = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
      Point point = webElement.getLocation();
      int width = webElement.getSize().getWidth();
      int height = webElement.getSize().getHeight();
      Rectangle rect = new Rectangle(width, height);
      BufferedImage img = ImageIO.read(screen);;
      BufferedImage dest = img.getSubimage(point.getX(), point.getY(), rect.width, rect.height);
      ImageIO.write(dest, "png", screen);
      String filename = System.nanoTime() + ".png";
      FileUtils.copyFile(screen, new File(filename));

Anirudha Chavan

unread,
Jun 1, 2015, 3:03:14 AM6/1/15
to seleniu...@googlegroups.com
Hi Nikesh,
This is what I am doing for Angular JS pages, but screenshot is not appearing for the element which I wanted. Its perfectly working fine for JSF pages.
Please let me know if you can help on this.

Nikesh Jauhari

unread,
Jun 1, 2015, 3:14:56 PM6/1/15
to seleniu...@googlegroups.com
How do you wait for your Angular JS calls to finish loading the page ?

Anirudha Chavan

unread,
Jun 2, 2015, 9:11:42 AM6/2/15
to seleniu...@googlegroups.com
I am not waiting for Angular JS calls to finish, I am taking first full screenshot and then cropping it.
If taken full screenshot , it showing correct GUI/text on the page, problem is in cropping them by elements position/size. So here I don't think I have to wait for Angular JS calls.
Let me know if I am wrong.

David

unread,
Jun 3, 2015, 1:21:48 AM6/3/15
to seleniu...@googlegroups.com
Then are you sure you have the right element (for position and size) to crop against?

Another thing to keep in mind, different browsers also render element position & size slightly differently. Therefore one browser may crop the screenshot and give you a result, another browser executing same code may give you a slightly different cropped image. It would be good to check the cropped screenshot across browsers to also see if the problem is specific to certain browser(s).

Anil Kumar Reddy Yarragonda

unread,
Aug 16, 2017, 11:24:07 AM8/16/17
to Selenium Users, anich...@gmail.com
@ Anirudha, 
 Did you fixed that issue? if you have any solution then please share with us.

Soumyodeep Karmakar

unread,
Oct 11, 2017, 9:32:35 AM10/11/17
to Selenium Users
This code is working in my monitor but while I am checking on my own 15" laptop it is not taking the full image screenshot. Help please. I am not able to fix this issue. Tried using scroll bar, window dimension change, changing the browser size, etc etc. Nothing worked! Please help ASAP.
Please send me a message on soumy...@gmail.com

File f1 = ((TakesScreenshot) d1).getScreenshotAs(OutputType.FILE); int ImageWidth = element.getSize().getWidth(); int ImageHeight = element.getSize().getHeight(); Point point = element.getLocation(); int xcord = point.getX(); int ycord = point.getY(); BufferedImage img = ImageIO.read(f1); BufferedImage dest = img.getSubimage(xcord, ycord, ImageWidth, ImageHeight); ImageIO.write(dest, "png", f1); FileUtils.copyFile(f1, new File("C:\\Users\\skarmaka\\Downloads\\b.png"));

Asim Sansi

unread,
Feb 9, 2020, 10:49:41 PM2/9/20
to Selenium Users
Anirudha Chavan, did you find any solution for this ? i know it's an really old post but please do let me know how you got around this issue

Rishikesh Patel

unread,
Feb 12, 2020, 6:09:42 PM2/12/20
to seleniu...@googlegroups.com
You can use tool like shutterbug or Ashot for element screenshot.

Thanks and regards,
Rishikesh Patel.
Mob - +91 90043 65654
Skype ID - Rishikesh9833
Email - rishik...@gmail.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 view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/1274960a-6967-4282-8280-1eb567b70362%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages