使用Robot()来处理弹出窗口的问题

时间:2017-08-22 12:00:44

标签: java selenium-webdriver

我正在使用Robot()来处理我的应用程序中弹出的窗口。我必须通过userIdpassworduserId成功输入,但相同的代码不适用于password。有什么我想念的吗?我正在使用Selenium和Webdriver。这是我的示例代码:

public void EnterUserId(String userId) {
    try {
        Thread.sleep(5000);
        Robot rb = new Robot();
        StringSelection username = new StringSelection(userId);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(username, null);
        rb.keyPress(KeyEvent.VK_CONTROL);
        rb.keyPress(KeyEvent.VK_V);
        rb.keyRelease(KeyEvent.VK_V);
        rb.keyRelease(KeyEvent.VK_CONTROL);
        rb.keyPress(KeyEvent.VK_TAB);
        rb.keyRelease(KeyEvent.VK_TAB);
        Reporter.reportStep("UserId has been entered", "PASS");
        Thread.sleep(5000);
    } catch (Exception e) {
        e.printStackTrace();
        Reporter.reportStep("UserId has not been entered", "FAIL");
    }
}

public void EnterPassword(String password) {
    try {
        Thread.sleep(5000);
        Robot rb = new Robot();
        StringSelection pwd = new StringSelection(password);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(pwd, null);
        rb.keyPress(KeyEvent.VK_CONTROL);
        rb.keyPress(KeyEvent.VK_V);
        rb.keyRelease(KeyEvent.VK_V);
        rb.keyRelease(KeyEvent.VK_CONTROL);
        rb.keyPress(KeyEvent.VK_TAB);
        rb.keyRelease(KeyEvent.VK_TAB);
        rb.keyPress(KeyEvent.VK_ENTER);
        rb.keyRelease(KeyEvent.VK_ENTER);
        Reporter.reportStep("Password has been entered", "PASS");
        Thread.sleep(5000);
    } catch (Exception e) {
        e.printStackTrace();
        Reporter.reportStep("Password has not been entered", "FAIL");
    }
}

1 个答案:

答案 0 :(得分:0)

如果我错了,请纠正我:你在这段代码中尝试CTRL + C和CTRl + V密码:

StringSelection pwd = new StringSelection(password);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(pwd, null);

setContents:      *将剪贴板的当前内容设置为指定的内容      * transferable对象并注册指定的剪贴板所有者      *作为新内容的所有者。*

但是密码字段经常受到保护以进行复制粘贴操作。因此,它不起作用。