0

I used the first answer to this question in order to adapt it to my need: saving pictures of a given URL on my laptop automatically.

My problem is how to get the URI of every image that exist on the webpage in order to complete my code correctly:

 import selenium
    class TestFirefox:
        def testFirefox(self):
            self.driver=webdriver.Firefox()
            # There are 2 pictures on google.com, I want to download them
            self.driver.get("http://www.google.com")
            self.l=[] # List to store URI to my images
            self.r=self.driver.find_element_by_tag_name('img')
            # I did print(self.r) but it does not reflect the URI of 
            # the image: which is what I want.

            # What can I do to retrieve the URIs and run this:
            self.l.append(self.image_uri)
            for uri_to_img in self.l:
               self.driver.get(uri_to_img)

               # I want to download the images, but I am not sure 
               # if this is the good way to proceed since my list's content
               # may not be correct for the moment 
               self.driver.save_screenshot(uri_to_image)
               driver.close()
    if __name__=='__main__':
        TF=TestFirefox()
        TF.testFirefox()
Community
  • 1
  • 1

1 Answers1

0

You need to get get src attribute of the given image in order to determine it's name and (possibly) address - remember, src can be also relative URI.

for img in self.l:
    url = img.get_attribute("src")

For downloading image you should try simple HTTP client like urllib

import urllib.request  

urllib.request.urlretrieve(url, "image.png")
Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
  • I am using Python-3.4 and it says urllib does not have a module named urlretrieve. Which one can work from this list: https://docs.python.org/3.3/library/urllib.html ? I used `request` but it says it is not callable object –  Jul 22 '14 at 13:12
  • 1
    I've edited my answer, this should work. Also note that this legacy code ported from Python 2, so for future compatibility use rather something like http://stackoverflow.com/questions/15846017/how-do-i-download-a-file-using-urllib-request-in-python-3 – Petr Mensik Jul 23 '14 at 08:36
  • I did not downvote you ! Believe me it is not me ! I can give you my password to here, you will check yourself i did not downvote !!!! Someone else did, not me –  Jul 23 '14 at 08:38
  • I believe you :-) But you can upvote or accept the answer if it was helpful to you. – Petr Mensik Jul 23 '14 at 09:05
  • Yes, you are in my mind, I will test the solution you gave me in less than 3 hours (i am coding for the moment something else), then i will see. –  Jul 23 '14 at 09:19
  • I tested your solution and combined it with what is discussed on the question of which you gave me a link. Yes, it works. Thank you very much Peter. –  Jul 23 '14 at 13:59