Save image to desire location

7,359 views
Skip to first unread message

Manoj Hans

unread,
Nov 27, 2012, 3:02:47 AM11/27/12
to seleniu...@googlegroups.com
Hi folks,

I m just sharing the code for saving image to desire location may be it will help many of u.

DownloadImage(By.xpath("//*[@id='content']/div/div[1]/div/div/div[1]/img"),
"D:\\Download\\image.png");

public void DownloadImage(By by,String loc) throws IOException{
WebElement Image=driver.findElement(by);
File screen=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        int width=Image.getSize().getWidth();
        int height=Image.getSize().getHeight();
        BufferedImage img=ImageIO.read(screen);
        BufferedImage dest=img.getSubimage(Image.getLocation().getX(), Image.getLocation().getY(), width, height);
        ImageIO.write(dest, "png", screen);
        File file=new File(loc);
         FileUtils.copyFile(screen,file);
}



--Manoj Hans

Manoj Hans

unread,
Nov 27, 2012, 5:21:29 AM11/27/12
to seleniu...@googlegroups.com
not able to understand wt do u want...can u explain wt exactly u want??



On Tuesday, November 27, 2012 3:04:34 PM UTC+5:30, abul hasan wrote:
How to match string/pattern in web Driver ? please help me. searching it for last 2 days.

praveen kandukuri

unread,
Nov 27, 2012, 8:12:28 AM11/27/12
to seleniu...@googlegroups.com
Hi Manoj Hans

  
   How it will execute, just explian how it works




 

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/3uBcqv_IUYQJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

Manoj Hans

unread,
Nov 28, 2012, 4:56:20 AM11/28/12
to seleniu...@googlegroups.com
@Praveen--

Using this method u can take screenshot of particular id,name etc...even u can save images without download.

u just need to call this method where u want to take screenshot of particular id.

DownloadImage("type here locator",
"type here location where u want to save screenshot");



--Manoj Hans

praveen kandukuri

unread,
Nov 28, 2012, 8:33:47 AM11/28/12
to seleniu...@googlegroups.com
Thanks manoj

To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/tuJml9dF8QUJ.

Kanchi

unread,
Aug 30, 2013, 3:30:40 AM8/30/13
to seleniu...@googlegroups.com
Hi Manoj,

I am getting dark screen when trying to take screenshot in chrome and IE , whereas the firefox captures the image quite well.

Please suggest any workarounds.

Thanks in Advance for your help.

Hanish Jadala

unread,
Aug 30, 2013, 4:10:08 PM8/30/13
to seleniu...@googlegroups.com
Hi

 I found that the problem is with chromedriver 2.x when I google it I found this way. the issue is the chromedriver. If am wrong kindly correct me



Regards,
Hanish


--
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.

David

unread,
Nov 22, 2013, 7:34:51 PM11/22/13
to seleniu...@googlegroups.com
I did see black cropped screenshot on IE 9 under Grid/remote mode. My local IE is IE10. Local IE worked fine. Using Selenium version 2.33.

FYI, I did notice differences in the cropping of the screenshots across browsers. So if you do anything more than just save the cropped images (e.g. do a hash compare, do some kind of image comparison), be aware that you have to account for some variance/tolerance, otherwise the comparison will fail. Less of a problem if you just support a single browser (and version). On a curious question has anyone on this thread actually do anything more with the image besides just saving it after cropping?

Avinash Kumar

unread,
Oct 29, 2014, 12:21:07 PM10/29/14
to seleniu...@googlegroups.com
Hello sir,

File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        BufferedImage  fullImg = ImageIO.read(screenshot);
        Point point = e.getLocation();
int eleWidth = e.getSize().getWidth();
int eleHeight = e.getSize().getHeight();
System.out.println(eleWidth);
System.out.println(eleHeight);
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(), eleWidth,eleHeight);
ImageIO.write(eleScreenshot, "jpg", screenshot);
FileUtils.copyFile(screenshot, new File("D://screenshot.jpg"));
-----------------------------------------------------------------------------------------------------------------------------------------------------
I m using this code to take a  screen shot of particular element. but i got an error like:
actually i want to take a screenshot of YT html 5 player "play/pause" button.

--------------------------------------------------------------------------------------------------------------------------------------------------------
Exception in thread "main" java.awt.image.RasterFormatException: (y + height) is outside of Raster
at sun.awt.image.ByteInterleavedRaster.createWritableChild(Unknown Source)
at java.awt.image.BufferedImage.getSubimage(Unknown Source)
at videoPlayer.Videoplayer.main(Videoplayer.java:42)
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Please help me ASAP.


your student :
Avinash kumar

On Tuesday, November 27, 2012 1:32:47 PM UTC+5:30, Manoj Hans wrote:

RUN Subi

unread,
May 20, 2016, 1:13:28 AM5/20/16
to Selenium Users
I tried this way, on my rMBP it fails to get the correct location. Anyone knows why?
在 2012年11月27日星期二 UTC+8下午4:02:47,Manoj Hans写道:

Venugopal Gadiparthi

unread,
Jul 5, 2018, 8:48:07 AM7/5/18
to Selenium Users


Hi Manoj,
Instead of taking screenshot  and crop from local system. Image available on the web page. We need to crop it while traversing to the application. How to do it?
Please find the below screenshot. 
Any suggestion?

Reply all
Reply to author
Forward
0 new messages