2

I download images from a webpage via WebDriver (Chrome) by

// STEP 1
$driver->get($link);

// STEP 2
$els=$driver->findElements(WebDriver\WebDriverBy::tagName('img'));

foreach ($els as $el) {
$src=$el->getAttribute('src');
$image=file_get_contents($src);
file_put_contents("image.jpg",$image);
}

While the images have already been loaded by the browser, I need to download the images again in STEP 2.

I can save the images after STEP 1 by right-click in the browser and Save image as ... without an internet connection because the images are available in the local cache of the browser.

Is it possible to save the images loaded by Chrome with WebDriver without downloading them again?

The above code is PHP, but any hit or example codes in other programming languages can solve the problem.

Googlebot
  • 15,159
  • 44
  • 133
  • 229
  • you can use selenium with python or java (there is more languages supported) and take a screen-shot of an element. – moe asal Jun 03 '20 at 14:27
  • @moeassal screenshot is not a rational way to save an image. The image is already on my local machine in the browser cache. I am sure there is a way to catch it. – Googlebot Jun 03 '20 at 14:39
  • Have you tried accessing cached data manually? from %appdata%\Local\Google\Chrome\User Data\Default\Cache – moe asal Jun 03 '20 at 14:52

1 Answers1

2

The below java code will download the image(or any file) in your desired directory.

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

public class imageDownload{

       public static void main(String[] args) throws IOException {
              URL img_url = new URL("image URL here");
              String fileName = img_url.getFile();
              String destName = "C:/DOWNLOAD/DIRECTORY" + fileName.substring(fileName.lastIndexOf("/"));

              InputStream is = img_url.openStream();
              OutputStream os = new FileOutputStream(destName);

              byte[] b = new byte[2048];
              int length;

              while ((length = is.read(b)) != -1) {
                     os.write(b, 0, length);
              }

              is.close();
              os.close();
       }
}

First, all the bytes stream of your image will be stored in the object 'is', and this bytes stream will be redirected to OutputStream object os to create a file(kind of copy-paste, but as a 0's and 1's).

Kalicharan.N
  • 134
  • 7