這是我的解決方法解決方案,如果有人碰巧在同一個問題上跌倒。 我無法讓webdriver釋放控制鍵,所以我讓機器人在每個發送鍵後釋放任何按下的控制鍵。使用崇高的文字生成,樂於使用。
/**
* 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-09 04:55:13
Joe
有一種簡潔的方式來釋放一個和絃控制鍵; [看我的答案](http://stackoverflow.com/a/22496445/59087)。 –