【问题标题】:How to download an image with selenium ?如何使用 selenium 下载图像?
【发布时间】:2017-12-19 03:52:00
【问题描述】:

from selenium import webdriver
from selenium.webdriver import ActionChains

def isExistsByXpath(xpath):
    try:
        element = driver.find_element_by_xpath(xpath)
        return True
    except Exception as e:
        return False

driver = webdriver.Firefox() 
driver.implicitly_wait(30)  
driver.get("http://callback.ganji.com/firewall/valid/920573663.do?namespace=ganji_hy_detail_pc&url=http%3A%2F%2Fanshan.ganji.com%2Fzhiyepeixun%2F944875012x.htm")
while not isExistsByXpath('//img[@class="dvc-captcha__bgImg"]'):
    driver.find_element_by_xpath('//input[@id="btnSubmit"]').click()
element = driver.find_element_by_xpath('//img[@class="dvc-captcha__bgImg"]')
action = ActionChains(driver)
action.move_to_element(element).context_click(element).perform()

如何右键下载图片?我尝试使用 'send_keys' 但不起作用。

请帮忙

  • python 3.6
  • ubuntu 17.04

【问题讨论】:

标签: python selenium


【解决方案1】:

试试类似的方法

import urllib
from selenium import webdriver

driver = webdriver.Chrome()
driver.get('YOUR_URL')

# get the image source
image_path = driver.find_element_by_xpath('IMAGE_XPATH')
src = img.get_attribute('src')

# download the image
urllib.urlretrieve(src, "captcha.png")

driver.close()

【讨论】:

    猜你喜欢
    • 2015-11-07
    • 2013-06-26
    • 1970-01-01
    • 2011-10-12
    • 2022-01-16
    • 2018-09-06
    • 2021-05-19
    • 2018-05-05
    • 2017-02-06
    相关资源
    最近更新 更多