0

I want to download and save images using selenium in python2.7

I've tried:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

url= "https://in.images.search.yahoo.com/images/view;_ylt=A2oKiHPRis1VplIALaEO9olQ;_ylu=X3oDMTIyN2I2OHZkBHNlYwNzcgRzbGsDaW1nBG9pZANjN2U1ZjU4NjAwMDQ1MDA0OGExZGMxY2Y0MzMyMDk0MwRncG9zAzEEaXQDYmluZw--?.origin=&back=https%3A%2F%2Fin.images.search.yahoo.com%2Fyhs%2Fsearch%3Fp%3D%2522Eiffel%2BGreens%2522%2BBalewadi%2509Pune%26n%3D60%26ei%3DUTF-8%26y%3DSearch%26type%3Dff.40.w81.hp.04-01.in.avg._.0715av%26fr%3Dyhs-avg-fh_lsonsw%26fr2%3Dsb-top-in.images.search.yahoo.com%26hsimp%3Dyhs-fh_lsonsw%26hspart%3Davg%26tab%3Dorganic%26ri%3D1&w=556&h=309&imgurl=www.propertyonepune.com%2Fimg%2Fgallery%2F0becda3e53f8db646a699e54b1333a4c.jpg&rurl=http%3A%2F%2Fwww.propertyonepune.com%2Fproperties%2F46%2FBalewadi&size=49.8KB&name=...+bungalows+by+Eiffel+Developers+%26+Realtors+Ltd.+at+%3Cb%3EBalewadi%3C%2Fb%3E%2C+%3Cb%3EPune%3C%2Fb%3E&p=%22Eiffel+Greens%22+Balewadi%09Pune&oid=c7e5f586000450048a1dc1cf43320943&fr2=sb-top-in.images.search.yahoo.com&fr=yhs-avg-fh_lsonsw&tt=...+bungalows+by+Eiffel+Developers+%26+Realtors+Ltd.+at+%3Cb%3EBalewadi%3C%2Fb%3E%2C+%3Cb%3EPune%3C%2Fb%3E&b=0&ni=21&no=1&ts=&tab=organic&sigr=11lu74lc1&sigb=17t67hvmu&sigi=1284god0v&sigt=12i2gtekb&sign=12i2gtekb&.crumb=wZ3uTmSmDfL&fr=yhs-avg-fh_lsonsw&fr2=sb-top-in.images.search.yahoo.com&hsimp=yhs-fh_lsonsw&hspart=avg&type=ff.40.w81.hp.04-01.in.avg._.0715av"
driver = webdriver.Firefox()
driver.get(url)

path = '//div[@class="iholder"]//img[@src]'
for k in driver.find_elements_by_xpath(path):
    items = []
    src = (k.get_attribute('src')).encode('utf8')
    items.append(src)
    print items
    for lm in items:
        driver.get(lm)
        driver.sendKeys(Keys.Control + "s")
        driver.send_keys(Keys.Enter)

It's giving me error:

Traceback (most recent call last):
File "C:/Users/Heypillow/Desktop/download.py", line 17, in <module>
driver.sendKeys(Keys.Control + "s")
AttributeError: 'WebDriver' object has no attribute 'sendKeys'

I've tried with:

driver.send_keys(Keys.CONTROL + "s")

Same error is showing
What should I do to save the images? Thanks in advance

4 Answers4

2

It looks like you want to save the html for each picture, so you could use actions to get the context-menu of firefox -> "p" is shortcut for save page:

for lm in items:
        driver.get(lm)
        body = driver.find_element(By.tagName("body"));
        ActionChains(driver).move_to_element(body).context_click(htmlElement).send_keys("p").send_keys(Keys.RETURN).perform();

I'm usually using Java, so there might be some typos in this python code of mine ;-)

drkthng
  • 6,651
  • 7
  • 33
  • 53
2

Actually, op's first attempt is more correct than the selected answer. If you're not sending keys to an element for typing then you're sending them to the browser for shortcuts, etc.

ActionChains(driver).key_down(Keys.Control).send_keys("s").key_up(Keys.Control)‌​‌​.perform()
sam-6174
  • 3,104
  • 1
  • 33
  • 34
1

The error you are getting is because .send_keys does not hang off of webdriver, it hangs off of webelement. You need to get a webelement first before trying to use .send_keys. For example,

for lm in items:
    lm.sendKeys(Keys.Control + "s")

This isn't going to answer your main question but it does explain why you are getting the error message.

To answer your main question, google it and you will find many responses such as this one that already has answers.

Community
  • 1
  • 1
JeffC
  • 22,180
  • 5
  • 32
  • 55
  • thank you it solved my problem.. I was stuck between the concept of Webdriver and Webelement thing I believe. – Ritobroto Mukherjee Aug 17 '15 at 06:53
  • 1
    this answer is not best practice (see mine below), and it could lead to race conditions with the timing of ctrl+s – sam-6174 Aug 18 '15 at 22:01
  • The code example is not an answer, it's a code sample to demonstrate the cause of the error... which I thought was clearly stated. It should NOT be used as is because it would spam CTRL+S to all elements in `items` in a matter of a few ms. – JeffC Aug 19 '15 at 14:21
  • No worries... my post was really for people coming across this from googling (there are a lot of screwy things with op's method of attack). I've just seen too many people using send_keys for keyboard shortcuts – sam-6174 Aug 19 '15 at 18:11
0

The Accepted Answer has had one change since 2015.

Instead of

Keys.Control

It has now changed to

Keys.CONTROL

and the snippet changes to

ActionChains(browser).key_down(Keys.CONTROL).send_keys("s").key_up(Keys.CONTROL).perform()