使用selenium

时间:2015-08-14 08:08:36

标签: python python-2.7 selenium

我想在python2.7中使用selenium下载并保存图像

我试过了:

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)

它给了我错误:

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'

我试过了:

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

显示相同的错误 如何保存图片?提前致谢

4 个答案:

答案 0 :(得分:1)

看起来你想为每张图片保存html,所以你可以使用动作来获取firefox的上下文菜单 - &gt; “p”是保存页面的快捷方式:

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();

我通常使用Java,所以在我的python代码中可能会有一些拼写错误; - )

答案 1 :(得分:1)

您收到的错误是因为.send_keys没有挂起webdriver,它会挂起webelement。在尝试使用webelement之前,您需要先获得.send_keys。例如,

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

这不会回答您的主要问题,但它确实解释了您收到错误消息的原因。

要回答您的主要问题,请点击它,您会发现很多回复,例如this one已经有答案。

答案 2 :(得分:1)

实际上,op的第一次尝试比选择的答案更正确。如果您没有将键发送到元素进行输入,那么您将它们发送到浏览器以获取快捷方式等。

ActionChains(driver).key_down(Keys.Control).send_keys("s").key_up(Keys.Control)‌​‌​.perform()

答案 3 :(得分:0)

已接受的答案自 2015 年以来发生了一个变化。

代替

Keys.Control

现在变成了

Keys.CONTROL

并且代码段更改为

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