0

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

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 文档似乎表明存在一个问题

注意:当低级 webdriver 键处理程序看到 Keys.NULL 时,活动修饰键 (CTRL/ALT/SHIFT/etc) 通过 keyup 事件释放。问题:http://code.google.com/p/webdriver/issues/detail?id=79

但是那个链接坏了……

硒 2.3 火狐 23

给。

更新

找到了这些:

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

Webdriver - java - 如何在执行操作后释放键

4

2 回答 2

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);
                }
            }
        }
    }
于 2013-10-09T04:55:13.143 回答
0

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

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

driver.findElement(By.cssSelector("body")).sendKeys(Keys.ESCAPE);
于 2014-11-18T04:41:51.723 回答