我想要一个复制到剪贴板的文本,并希望将其粘贴到文本字段中。
有人可以告诉我怎么做吗
代表: -
driver.get("https://mail.google.com/");
driver.get("https://www.guerrillamail.com/");
driver.manage().window().maximize();
driver.findElement(By.id("copy_to_clip")).click(); -->copied to clipboard
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.id("nav-item-compose")).click();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.name("to")).???;//i have to paste my text here that is copied from above
答案 0 :(得分:4)
如果点击ID为' copy_to_clip'的按钮真的将内容复制到剪贴板然后你可以使用键盘快捷方式选项。我想,您可能没有尝试过模拟CTRL + v组合。单击它激活目标文本字段,然后使用快捷方式。这可能会有所帮助。
代码段:
driver.findElement(By.name("to")).click(); // Set focus on target element by clicking on it
//now paste your content from clipboard
Actions actions = new Actions(driver);
actions.sendKeys(Keys.chord(Keys.LEFT_CONTROL, "v")).build().perform();
答案 1 :(得分:1)
根据您的问题,因为您在剪贴板中已经有一些 text 并将其粘贴到 text 字段中,您可以使用{ {3}}方法,您可以使用以下解决方案:
//required imports
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
//other lines of code
driver.findElement(By.id("copy_to_clip")).click(); //text copied to clipboard
String myText = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor); // extracting the text that was copied to the clipboard
driver.findElement(By.name("to")).sendKeys(myText);//passing the extracted text to the text field
答案 2 :(得分:1)
Pyperclip非常适合将文本复制到剪贴板-https://pypi.org/project/pyperclip/
将一些文本复制到剪贴板后,请使用pyperclip.paste()进行检索。
答案 3 :(得分:0)
This worked well for me when pasting emoji into a text box
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
String emoji="☺️??❤️????????????";
StringSelection stringSelection = new StringSelection(emoji);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);
WebElement input = your_driver.findElementByXPath(your_xpath);
input.sendKeys(Keys.SHIFT, Keys.INSERT);
答案 4 :(得分:0)
我想出了这一点来使用剪贴板API测试粘贴。问题的很大一部分是截至12/2020为止需要被入侵的权限:
// Setup web driver
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<>();
Map<String, Object> profile = new HashMap<>();
Map<String, Object> contentSettings = new HashMap<>();
Map<String, Object> exceptions = new HashMap<>();
Map<String, Object> clipboard = new HashMap<>();
Map<String, Object> domain = new HashMap<>();
// Enable clipboard permissions
domain.put("expiration", 0);
domain.put("last_modified", System.currentTimeMillis());
domain.put("model", 0);
domain.put("setting", 1);
clipboard.put("https://google.com,*", domain); // <- Replace with test domain
exceptions.put("clipboard", clipboard);
contentSettings.put("exceptions", exceptions);
profile.put("content_settings", contentSettings);
prefs.put("profile", profile);
options.setExperimentalOption("prefs", prefs);
// [...]
driver.executeScript("navigator.clipboard.writeText('sample text');");
pasteButton.click();
在浏览器中运行的Javascript:
// Button handler
navigator.clipboard.readText().then(t-> console.log("pasted text: " + t));