2013-10-09 65 views
0

此代碼選擇兩個文本框,因爲它似乎沒有釋放控制鍵。Selenium keys.chord不釋放控制鍵

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鍵一樣操作。

硒文檔似乎表明現有的問題

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 火狐23

放棄。

更新

找到了這些:

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

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

回答

0

這是我的解決方法解決方案,如果有人碰巧在同一個問題上跌倒。 我無法讓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); 
       } 
      } 
     } 
    } 
+0

有一種簡潔的方式來釋放一個和絃控制鍵; [看我的答案](http://stackoverflow.com/a/22496445/59087)。 –

0

我剛想出一個解決方法。

我不知道它是否影響其他事情,但它對我來說工作正常。

driver.findElement(By.cssSelector("body")).sendKeys(Keys.ESCAPE); 
相關問題