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.
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!
halfer1 Answer
Not the answer you're looking for? Browse other questions tagged seleniumselenium-webdriver or ask your own question.
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?
FidelFidelSelenium 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
mecekmecekYes, you do this in several steps:
- Take a screenshot of the webpage and save it to disk
- Find the image element
- Find the image element location, width and height
- Crop the image you need from the screenshot you took in step 1
- 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
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.
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.
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.
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_idiotHave you trying to download the image using ImageIO?
JohnnyJohnny