【问题标题】:Saving Images from a URL in selenium (Python)在 selenium (Python) 中从 URL 保存图像
【发布时间】:2016-03-18 11:12:44
【问题描述】:

所以,我终于完成了从某个站点获取一些图像链接的简单脚本。现在,问题是 selenium 没有保存这些图像。不,我不想要屏幕截图,它们没有达到目的。屏幕截图在它自己的分辨率下会留下很多空白。

无论如何,我已经尝试发送“ctrl+S”,然后按“Enter”:

saveas = ActionChains(driver).key_down(Keys.CONTROL).send_keys('s').key_up(Keys.CONTROL).key_down(Keys.ENTER).key_up(Keys.ENTER)
        saveas.perform()

现在,我在这里做错了什么?图片链接:http://imgcomic.naver.net/webtoon/654817/44/20160314180903_2cfdd685f7e4f2c93a54e819898d6fb1_IMAG01_2.jpg

【问题讨论】:

  • 我无法访问您附加的链接。

标签: python python-2.7 selenium sendkeys


【解决方案1】:

Webdriver 截取整个页面。但是,您可以尝试根据元素尺寸裁剪图像。下面的java代码会帮助你

 driver.get("http://www.google.com");
    WebElement image = driver.findElement(By.id("hplogo"));   
    //Get entire page screenshot
    File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    BufferedImage  fullImg = ImageIO.read(screenshot);
    //Get the location of element on the page
    Point point = image.getLocation();
    //Get width and height of the element
    int imageWidth = image.getSize().getWidth();
    int imageHeight = image.getSize().getHeight();
    //Crop the entire page screenshot to get only element screenshot
    BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(), imageWidth,
            imageHeight);
    ImageIO.write(eleScreenshot, "png", screenshot);
    //Copy the element screenshot to disk
    FileUtils.copyFile(screenshot, new File("E:\\selenium_desk\\GoogleLogo_screenshot1.png"));
    driver.close();

谢谢你, 壁画

【讨论】:

    【解决方案2】:

    您可以仅使用图像 URL 创建另一个 webdriver 实例并截取屏幕截图。不理想,但大多数时候对我有用。

    fb = webdriver.Firefox()
    fb.set_window_size(1200,2000) # Make sure the browser size is large enough
    fb.get('IMAGE_URL')
    imgs = fb.find_elements_by_tag_name('img')
    imgs[0].screenshot('screenshot.png')
    

    如果大小很重要,您可以使用 Pillow 将图像转换为 jpg。

    【讨论】:

      猜你喜欢
      • 2015-07-25
      • 1970-01-01
      • 1970-01-01
      • 2012-03-30
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      • 2010-10-17
      相关资源
      最近更新 更多