Save image from url with HTMLUnit

Arya picture Arya · May 19, 2012 · Viewed 9.1k times · Source

Is it possible to save an image to hard disk with HTMLUnit by giving it the image URL? If so how?

Regards!

Answer

Mosty Mostacho picture Mosty Mostacho · May 19, 2012

If you're using HtmlUnit then you should have an HtmlPage. There you can get an HtmlImage and save the file this way:

HtmlImage image = page.<HtmlImage>getFirstByXPath("//img[@src='blah']");
File imageFile = new File("/path/to/file.jpg");
image.saveAs(imageFile);

If you do have an URL... then I don't think you need HtmlUnit to download the image.