简体   繁体   English

Selenium Java-将图像从URL复制到剪贴板并将其粘贴为图像

[英]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/ 我想从URL复制图像/ gif,而不下载它,例如,我想将此gif复制到剪贴板https://media.giphy.com/media/l49JL8rJ2vOEXlmM0/giphy.gif并将此gif粘贴到此站点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 粘贴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 贴上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 如果这样不起作用,您可以尝试在页面上的某个元素上执行ctrl + v,例如

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? 这些帮助有用?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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