1

For my project I need to copy image (not url, image name. Only data for ability, for example, to paste it to "Microsoft Paint") from page to clipboard by Chrome console.

I tried this:

copy(document.getElementById('someimage'));

but it returns nothing... It only works with text.


If you don't know, then how to download this image by chrome console?

OR

How to make screenshot of the page and copy or download it using Chrome console?

P.s. I can't use any js libraries.

ILya
  • 21
  • 1
  • 5

2 Answers2

0

As you mentioned you are using Selenium, here's how to save an image with Selenium:

You need to get the image's URL, load it (using ImageIO in this example) and save it. For example, in Java you would do something like this:

try {
    driver = new ChromeDriver();
    driver.get("http://...");

    WebElement img = driver.findElement(By.cssSelector("#selector"));
    BufferedImage buffer = ImageIO.read(new URL(img.getAttribute("src")));
    ImageIO.write(buffer, "png", new File("image.png"));
} catch (Exception e) {
    e.printStackTrace();
} finally {
    driver.close();
}

If you want to copy it directly, your class needs to implement java.awt.datatransfer.ClipboardOwner and then you would do something like this:

try {
    driver = new ChromeDriver();
    driver.get("http://...");

    WebElement img = driver.findElement(By.cssSelector("#selector"));
    TransferableImage transferable = new TransferableImage(ImageIO.read(new URL(img.getAttribute("src"))));
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(transferable, this );
} catch (Exception e) {
    e.printStackTrace();
} finally {
    driver.close();
}

Regarding your other questions, here's how to take a screenshot using Chrome DevTools:

There are 3 Capture... commands in Chrome DevTools. Just follow these steps to get to them:

  1. Open DevTools.

  2. Go to the Elements tab and click on the element you want to take the screenshot of.

  3. Press Cmd + P on Mac or Ctrl + P on Windows.

  4. Type > screen. You will get 3 relevant suggestions:

    • Mobile Capture full size screenshot: Captures the whole page, including the non-visible (out of viewport) area.

    • Mobile Capture node screenshot: Captures a single node, in this case, the element you clicked in the second step.

    • Mobile Capture screenshot: Captures the visible area of the page (viewport).

  5. Click on any of them and the screenshot will download automatically.

However, keep in mind this feature doesn't always work fine, especially the Capture node screenshot one, so it might be better to capture the visible area of the page and crop the afterwards.

Danziger
  • 19,628
  • 4
  • 53
  • 83
  • This answer is for your last question: _How to make screenshot of the page and copy or download it using Chrome console?_ That's the closer thing available in DevTools, even though it's not on the Console tab. – Danziger Aug 27 '18 at 12:18
  • @ILya Then this is what you could do, but it doesn't work in all the cases: https://stackoverflow.com/questions/17527713/force-browser-to-download-image-files-on-click/17527821 – Danziger Aug 27 '18 at 16:39
0

I have explored few things in chrome dev tools

https://homeguides.sfgate.com/stop-air-flow-ceiling-air-diffuser-28867.html - This is the website I am using it for reference.

  1. In Chrome console try the following command

enter image description here

imageurl= document.getElementsByTagName('img')[0].currentSrc;
copy (imageurl);

Note: Here you can change the img [1] array if you want to get different images

  1. Then if you press ctrl + v in your keyboard you could see the image url with https. Please see the above screenshot.

  2. You can perform the ctrl+ v on your new tab to get the image loaded.

or You can try the following method.

  1. Right click the image and click inspect element
  2. You could see some image url. Copy that URL

enter image description here

  1. Open new Tab and paste the URL
  2. If you right click on it you can see "Save Images" option.

Hope it will help you in someway.

Ragavan Rajan
  • 4,171
  • 1
  • 25
  • 43
  • 1
    No, I need image data, for example, to paste it to "Paint". – ILya Aug 27 '18 at 11:24
  • @ILya Why can't you just right-click the image and click _Copy Image_? – Danziger Aug 27 '18 at 12:47
  • 1
    Because I use remote chrome control which allows use only console commands (I know about selenium) – ILya Aug 27 '18 at 14:12
  • @ILya Ok, then you might just use Selenium to copy or download the image instead. Take a look at my updated answer, maybe that's what you are looking for. – Danziger Aug 27 '18 at 16:28
  • selenium is not good for my project. As I mentioned, I can use only chrome console commands (or js). – ILya Aug 27 '18 at 16:31