Selenium Webdriver Download Image

Here is a trick how to download any image from a page. Contact; How to download images using WebdriverIO (Selenium Webdriver. Downloading images using Webdriver. How to Download & Install Selenium WebDriver. Download the Selenium Java Client Driver. Your Properties dialog should now look similar to the image below.

Selenium Webdriver Download Image
Active1 year, 3 months ago

I'm learning automation these days. So I was wondering if there any way I can download images from a dynamic website using Selenium? I'm using Java for this.

I'm able to get the links to about 40 images but not all. I don't know how dynamic website works but I think some of the links gets loaded/shown when the user is scrolling through the page or something like that!

halfer
15.3k7 gold badges63 silver badges129 bronze badges
Anurag BhagsainAnurag Bhagsain

1 Answer

mbn217mbn217
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.

Not the answer you're looking for? Browse other questions tagged seleniumselenium-webdriver or ask your own question.

Active3 months ago

I'm using Selenium & Google Chrome Driver to open pages programatically. On each page there is a dynamically generated image which I'd like to download. At the moment, I'm waiting for the page to finish loading, then I grab the image URL and download it using System.Net.WebClient.

That works fine except I'm downloading the images twice - once in the browser, once with WebClient. The problem is that each image is roughly 15MB and downloading twice adds up quickly.

So - is it possible to grab the image straight from Google Chrome?

FidelFidel
3,2286 gold badges38 silver badges54 bronze badges

Selenium Webdriver Internet Explorer Driver

7 Answers

One way is to get base64 string of the image with javascript that is executed by webdriver. Then you can save base64string of the image to file.

Basically, if your image is

then you can convert it like

mecekmecek
1,9331 gold badge14 silver badges23 bronze badges

Yes, you do this in several steps:

  1. Take a screenshot of the webpage and save it to disk
  2. Find the image element
  3. Find the image element location, width and height
  4. Crop the image you need from the screenshot you took in step 1
  5. Save the image to disk (or do something else with it)

Sample code - please add your code to catch exceptions

the CropImage method was posted by James Hill, How to cut a part of image in C#

but I will add it here as well for clarity

Community
TH TodorovTH Todorov

All the above answers work. However, they all have limitations. mecek's method is cool, but it only works on browsers that support html 5 (although most browsers now do), and it will downgrade the image quality. The screenshot method will also downgrade image quality. Using System.Net.WebClient can avoid this issue, but won't work in the case of downloading a captcha image. Actually the only way that works for me when downloading a captcha image is using the Actions class (or Robot if you are using Selenium's java version), something like below:

This is the only way I've found to download a captcha image without losing its quality (for better OCR effects) using Selenium Chrome driver, although the limitation is also obvious.

Silent SojournerSilent Sojourner

Based on meceks answer, I use a version of the following with great results to capture the webdriver image.

It creates a base64 jpeg string at 90% quality. To avoid pixelation issues, i draw the image onto a canvas which is larger than what i will be presenting the image on later. The image is therefore up-scaled to best fit a box of 600 pixels while preserving aspect ratios.Since jpeg doesn't support transparency i clear the context with a white background.

AdamAdam

You can block images from being downloaded in Google Chrome using this technique. It runs a Google Chrome extension called 'Block Image'. This way the image won't be downloaded using chrome, and it's just a matter of downloading the image as normal using its URL & System.Net.WebClient.

Community
FidelFidel
3,2286 gold badges38 silver badges54 bronze badges

Talks of selenium.

Htmlunit ?

Anyways, why don't you use webclient (htmlunit-driver) or pure htmlunit (http://htmlunit.sourceforge.net/). Htmlunit doesn't download images by default.

You can download them on will, as per your requirement.

coding_idiotcoding_idiot
9,9898 gold badges46 silver badges101 bronze badges

Have you trying to download the image using ImageIO?

JohnnySelenium Webdriver Download ImageJohnny
5,8926 gold badges48 silver badges81 bronze badges
Jxl jar for selenium webdriver download

Firefox Selenium Webdriver Download

Not the answer you're looking for? Browse other questions tagged c#seleniumdownloadselenium-chromedriver or ask your own question.