Active7 months ago

I am trying to download a captcha image with Selenium, however, I'm getting a different image downloaded than the one showed in the browser.If I try to download the image again, without changing the browser, I get a different one.

Any thoughts?

Selenium get images. From selenium import webdriver options. That will return all the image urls on the webpage. To download import urllib and use the. 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. 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.

Ratmir Asanov
3,8453 gold badges13 silver badges27 bronze badges
fmarquesfmarques

3 Answers

Because the link of image's src gives you a random new captcha image once you open that link!

Instead of download the file from the image's src, you can take a screenshot to get the one in browser. However, you need to download Pillow (pip install Pillow) and use it like the way mentioned in this answer:

(Note that I've changed the code little bit so it could works in your case.)

Community
Kevin GuanKevin Guan
14.8k10 gold badges44 silver badges71 bronze badges

You can get the rendered image of the captacha with a piece of Javascript. It is faster than taking and cropping a screenshot:

Florent B.Florent B.
29.8k4 gold badges42 silver badges71 bronze badges

If you already have image loaded, instead of execute_async_script, go with

Anatoly AlekseevAnatoly Alekseev

Not the answer you're looking for? Browse other questions tagged pythonselenium 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

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

Free

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. Usb 2.0 web camera driver.

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

Selenium Webdriver Download For Mac

FidelFidel
3,2286 gold badges38 silver badges54 bronze badges

Selenium Ide Download

Talks of selenium.

Jxl Jar For Selenium Webdriver Download

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?

JohnnyJohnny
5,8926 gold badges48 silver badges81 bronze badges

Selenium Webdriver Download Image

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