简体   繁体   中英

Selenium Java - Copy Image from URL to clipboard and Paste it as image

I want to copy image/gif from URL without download it, for example, I want to copy this gif to clipboard https://media.giphy.com/media/l49JL8rJ2vOEXlmM0/giphy.gif and paste this gif to this site https://paste.pics/

I tried everything but it's not working.

Code trials : Copy to clipboard :

StringSelection data = new StringSelection ("https://media.giphy.com/media/xThtap5F0MFyAkoBPi/giphy.gif"); 
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard(); 
cb.setContents(data, data);

Paste 1

try {
        Transferable t = cb.getContents(null);
        if (t.isDataFlavorSupported(DataFlavor.stringFlavor))
            System.out.println(t.getTransferData(DataFlavor
                    .stringFlavor));
        driver.findElement(By.xpath("elementFromSite")).sendKeys(t.getTransferData(DataFlavor
                .imageFlavor).toString());
    } catch (UnsupportedFlavorException | IOException ex) {
        System.out.println("issue");
    }

Paste 2

driver.findElement(By.id(elementFromSite)).sendKeys(Keys.chord(Keys.CONTROL,"v"));

so I don't have any idea how can I achieve it Please assist

Here's what I would try:

driver.get("https://media.giphy.com/media/l49JL8rJ2vOEXlmM0/giphy.gif");
driver.find_by_css_selector('a._3X9Zhs_atixoQRBDsNGQnl > img').sendKeys(Keys.CONTROL+ "c");

driver.get("https://paste.pics/");
Actions action = new Actions(driver); 

action.keyDown(Keys.CONTROL).sendKeys("v").keyUp(Keys.CONTROL).perform();

If that doesn't work you could try doing ctrl+v on an element on the page, like

driver.find_element_by_css_selector('whatever selector you want to target some element on the page').sendKeys(Keys.CONTROL+ "v");

Does that help at all?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM