Selenium keys.chord不释放控制键

时间:2013-10-09 01:25:27

标签: firefox selenium

此代码选择两个文本框,因为似乎控制键未被释放。

findElement('#textbox-wrapper').click();
findElement('#textbox').sendKeys(Keys.chord(Keys.CONTROL, "A"))
findElement('#textbox2-wrapper').click();

如果我注释掉中间的行,它会按顺序选择文本框,并在测试结束时按预期选择第二个文本框。

如果我注释掉两次点击并在sendKeys之后手动按浏览器窗口上的f,它会执行Ctrl + F查找,这样看起来sendKeys肯定不会释放ctrl。

有趣的是,发送另一个密钥findElement('#textbox').sendKeys("A")似乎可以解决问题,因为点击或按f后不再像按住ctrl一样。

selenium文档似乎暗示了一个现有问题

Note: When the low-level webdriver key handlers see Keys.NULL, active modifier keys (CTRL/ALT/SHIFT/etc) release via a keyup event. Issue: http://code.google.com/p/webdriver/issues/detail?id=79

但该链接已损坏......

硒2.3 Firefox 23

提供。

更新

找到这些:

http://selenium.10932.n7.nabble.com/Release-ctrl-key-using-Actions-td25378.html

Webdriver - java - how to release keys after performing an action

2 个答案:

答案 0 :(得分:0)

如果有人碰巧遇到同样的问题,这是我的解决方法。 我无法让webdriver释放控制键,所以我让机器人在每个sendkey后释放任何按下的控制键。使用崇高的文本生成,愉快使用。

    /**
     * Give a sequence of characters, will find control keys and use the java awt robot to keyRelease these control keys.
     * The firefox web driver doesn't release control keys properly causing subsequent actions to be performed with these
     * keys still pressed.
     */
    private void RobotReleaseKeys(CharSequence[] charSequences) throws AWTException {
        Robot rob = new Robot();
        for (CharSequence charSequence: charSequences) {
            for (char c: charSequence.toString().toCharArray()) {
                if (c == Keys.CONTROL.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_CONTROL);
                } else if (c == Keys.DELETE.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_DELETE);
                } else if (c == Keys.END.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_END);
//                } else if (c == Keys.NULL.charAt(0)) {
//                  rob.keyRelease(KeyEvent.VK_NULL);
                } else if (c == Keys.CANCEL.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_CANCEL);
                } else if (c == Keys.HELP.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_HELP);
                } else if (c == Keys.BACK_SPACE.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_BACK_SPACE);
                } else if (c == Keys.TAB.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_TAB);
                } else if (c == Keys.CLEAR.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_CLEAR);
//                } else if (c == Keys.RETURN.charAt(0)) {
//                  rob.keyRelease(KeyEvent.VK_RETURN);
                } else if (c == Keys.ENTER.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_ENTER);
                } else if (c == Keys.SHIFT.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_SHIFT);
//                } else if (c == Keys.LEFT_SHIFT.charAt(0)) {
//                  rob.keyRelease(KeyEvent.VK_LEFT_SHIFT);
                } else if (c == Keys.CONTROL.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_CONTROL);
//                } else if (c == Keys.LEFT_CONTROL.charAt(0)) {
//                  rob.keyRelease(KeyEvent.VK_LEFT_CONTROL);
                } else if (c == Keys.ALT.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_ALT);
//                } else if (c == Keys.LEFT_ALT.charAt(0)) {
//                  rob.keyRelease(KeyEvent.VK_LEFT_ALT);
                } else if (c == Keys.PAUSE.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_PAUSE);
                } else if (c == Keys.ESCAPE.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_ESCAPE);
                } else if (c == Keys.SPACE.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_SPACE);
                } else if (c == Keys.PAGE_UP.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_PAGE_UP);
                } else if (c == Keys.PAGE_DOWN.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_PAGE_DOWN);
                } else if (c == Keys.END.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_END);
                } else if (c == Keys.HOME.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_HOME);
                } else if (c == Keys.LEFT.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_LEFT);
                } else if (c == Keys.ARROW_LEFT.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_LEFT);
                } else if (c == Keys.UP.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_UP);
                } else if (c == Keys.ARROW_UP.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_UP);
                } else if (c == Keys.RIGHT.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_RIGHT);
                } else if (c == Keys.ARROW_RIGHT.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_RIGHT);
                } else if (c == Keys.DOWN.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_DOWN);
                } else if (c == Keys.ARROW_DOWN.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_DOWN);
                } else if (c == Keys.INSERT.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_INSERT);
                } else if (c == Keys.DELETE.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_DELETE);
                } else if (c == Keys.SEMICOLON.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_SEMICOLON);
                } else if (c == Keys.EQUALS.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_EQUALS);
                } else if (c == Keys.NUMPAD0.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_NUMPAD0);
                } else if (c == Keys.NUMPAD1.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_NUMPAD1);
                } else if (c == Keys.NUMPAD2.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_NUMPAD2);
                } else if (c == Keys.NUMPAD3.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_NUMPAD3);
                } else if (c == Keys.NUMPAD4.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_NUMPAD4);
                } else if (c == Keys.NUMPAD5.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_NUMPAD5);
                } else if (c == Keys.NUMPAD6.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_NUMPAD6);
                } else if (c == Keys.NUMPAD7.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_NUMPAD7);
                } else if (c == Keys.NUMPAD8.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_NUMPAD8);
                } else if (c == Keys.NUMPAD9.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_NUMPAD9);
                } else if (c == Keys.MULTIPLY.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_MULTIPLY);
                } else if (c == Keys.ADD.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_ADD);
                } else if (c == Keys.SEPARATOR.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_SEPARATOR);
                } else if (c == Keys.SUBTRACT.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_SUBTRACT);
                } else if (c == Keys.DECIMAL.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_DECIMAL);
                } else if (c == Keys.DIVIDE.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_DIVIDE);
                } else if (c == Keys.F1.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_F1);
                } else if (c == Keys.F2.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_F2);
                } else if (c == Keys.F3.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_F3);
                } else if (c == Keys.F4.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_F4);
                } else if (c == Keys.F5.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_F5);
                } else if (c == Keys.F6.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_F6);
                } else if (c == Keys.F7.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_F7);
                } else if (c == Keys.F8.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_F8);
                } else if (c == Keys.F9.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_F9);
                } else if (c == Keys.F10.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_F10);
                } else if (c == Keys.F11.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_F11);
                } else if (c == Keys.F12.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_F12);
                } else if (c == Keys.META.charAt(0)) {
                    rob.keyRelease(KeyEvent.VK_META);
//                } else if (c == Keys.COMMAND.charAt(0)) {
//                  rob.keyRelease(KeyEvent.VK_COMMAND);
//                } else if (c == Keys.ZENKAKU_HANKAKU.charAt(0)) {
//                  rob.keyRelease(KeyEvent.VK_ZENKAKU_HANKAKU);
                }
            }
        }
    }

答案 1 :(得分:0)

我刚刚想出了一个解决方法。

我不知道它是否会影响其他事情,但对我来说效果很好。

driver.findElement(By.cssSelector("body")).sendKeys(Keys.ESCAPE);