How to download an image using Selenium (any version)?

前端 未结 13 1587
夕颜
夕颜 2020-11-29 04:11

I was wondering, how can one use selenium/webdriver to download an image for a page. Assuming that the user session is required to download the image hence having pure URL i

13条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-29 04:53

    I prefer doing something like this :

    1. Get the SRC attribute of the image.
    2. Use ImageIO.read to read the image onto a BufferedImage
    3. Save the BufferedImage using ImageIO.write function
    

    For e.g.

    String src = imgElement.getAttribute('src');
    BufferedImage bufferedImage = ImageIO.read(new URL(src));
    File outputfile = new File("saved.png");
    ImageIO.write(bufferedImage, "png", outputfile);
    
    

提交回复
热议问题