如何使用 selenium webdriver 执行复制/粘贴 [英] how to execute copy/paste using selenium webdriver

查看:108
本文介绍了如何使用 selenium webdriver 执行复制/粘贴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 java 中的 selenium webdriver 执行粘贴到已存在的局部变量到输入文本.为此,我使用了这种方法:

I want to execute a paste to a local variable already exist to an input text with selenium webdriver in java. for this I used this method:

public static void copyText(final String id, final String text) throws Exception {
        waitForJQueryProcessing(DRIVER, N_30);
        WebElement elem = DRIVER.findElement(By.id(id));
        DRIVER.findElement(By.id(id)).clear();
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Clipboard clipboard = toolkit.getSystemClipboard();
        StringSelection strSel = new StringSelection(text);
        clipboard.setContents(strSel, null);
        elem.sendKeys(Keys.chord(Keys.CONTROL, "v", text));
        System.out.println(text);

    }

当我执行测试时,我会出现空我不知道为什么?

When I execute the test, i will appear empty I don't know why ?

推荐答案

当您尝试将 字符序列clipboard 复制到 ; 元素您可以使用以下解决方案:

As you are trying to copy the character sequence from clipboard to a <input> element you can use the following solution:

//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
WebElement elem = DRIVER.findElement(By.id(id));
elem.clear();
String data = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
elem.sendKeys(data);

这篇关于如何使用 selenium webdriver 执行复制/粘贴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆