Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download an image using Selenium (any version)?

I was wondering, how can one use selenium/webdriver to download an image for a page. Assuming that the user session is required to download the image hence having pure URL is not helpful. Any sample code is highly appreciated.

like image 640
Ali Salehi Avatar asked Jul 25 '11 08:07

Ali Salehi


1 Answers

I prefer doing something like this :

1. Get the SRC attribute of the image. 2. Use ImageIO.read to read the image onto a BufferedImage 3. Save the BufferedImage using ImageIO.write function 

For e.g.

String src = imgElement.getAttribute('src'); BufferedImage bufferedImage = ImageIO.read(new URL(src)); File outputfile = new File("saved.png"); ImageIO.write(bufferedImage, "png", outputfile);  
like image 55
coding_idiot Avatar answered Sep 22 '22 04:09

coding_idiot