Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

save an image with selenium & firefox

i'm trying to save an image from a website using selenium server & python client. i know the image's URL, but i can't find the code to save it, either when it's the the document itself, or when it's embedded in the current browser session.

the workaround i found so far is to save the page's screenshot (there are 2 selenium methods for doing just that), but i want the original image.

i don't mind fiddling with the clicking menu options etc. but i couldn't found how.

thanks

like image 548
Berry Tsakala Avatar asked May 03 '09 09:05

Berry Tsakala


People also ask

How do I download images from web using Selenium?

We can download images with Selenium webdriver in Python. First of all, we shall identify the image that we want to download with the help of the locators like id, class, xpath, and so on. We shall use the open method for opening the file in write and binary mode (is represented by wb).

Can Selenium automate images?

Selenium is a software library to locate elements on web page and interact with them. To deal with images, you need to use different library. Selenium can provide screenshots (images) but you need to use something else to work with such images.

How do I get Selenium images?

New Selenium IDE We can get the source of an image in Selenium. An image in an html document has <img> tagname. Each image also has an attribute src which contains the source of image in the page. To fetch any attribute in Selenium, we have to use the getAttribute() method.


2 Answers

I found code that puts an image in to a canvas, then converts it to data - which could then be base64 encoded for example. My thought was to call this using the eval command in selenium, however in my testing the toDataURL is throwing a security error 1000. Seems like it is so close to a solution if not for that error.

var data, canvas, ctx;
var img = new Image();
img = document.getElementById("yourimageID");
canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);  // everything works up to here
data = canvas.toDataURL();  // this fails ***
var base64Img = data.replace(/^data:image\/(png|jpg);base64,/, "");

Doing some research I found references that it is not allowed to use toDataURL when the image is from a different domain. However, I even tried this code by saving the page, stripping everything out except the image itself and this script.

For example (index.html) :

<html><head></head><body>
<img src="local/hard/disk/img.jpg" id="yourimageID">
<script>
// script from above
</script>
</body></html>

The img.jpg and index.html are stored locally, opening the page in firefox locally, still get a security error 1000!

like image 79
Scott Szretter Avatar answered Sep 28 '22 01:09

Scott Szretter


To do this the way you want (to actually capture the content sent down to the browser) you'd need to modify Selenium RC's proxy code (see ProxyHandler.java) and store the files locally on the disk in parallel to sending the response back to the browser.

like image 45
Patrick Lightbody Avatar answered Sep 27 '22 23:09

Patrick Lightbody