selenium模拟键盘操作

单键

1     /**
2      * 模拟键盘回车事件
3      * @throws AWTException
4      */
5     public void KeyEventEnter() throws AWTException {
6         Robot robot = new Robot();
7         robot.keyPress(KeyEvent.VK_ENTER);
8     }

组合键

 1     /**
 2      * 复制变量值到剪贴板,并粘贴
 3      * @throws AWTException
 4      */
 5     public void KeyEventClipboard(String str) throws AWTException {
 6         Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
 7         Transferable t = new StringSelection(str);
 8         cb.setContents(t, null);
 9 
10         Robot robot = new Robot();
11         robot.keyPress(KeyEvent.VK_CONTROL);
12         robot.keyPress(KeyEvent.VK_V);
13 
14         robot.keyRelease(KeyEvent.VK_V);
15         robot.keyRelease(KeyEvent.VK_CONTROL);
16     }

 

单键

    /**

     * 模拟键盘回车事件

     * @throws AWTException

     */

    public void KeyEventEnter() throws AWTException {

       Robot robot = new Robot();

       robot.keyPress(KeyEvent.VK_ENTER);

    }

组合键

/**

     * 复制变量值到剪贴板,并粘贴

     * @throws AWTException

     */

    public void KeyEventClipboard(String str) throws AWTException {

       Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();

       Transferable t = new StringSelection(str);

       cb.setContents(t, null);

 

       Robot robot = new Robot();

       robot.keyPress(KeyEvent.VK_CONTROL);

       robot.keyPress(KeyEvent.VK_V);

 

       robot.keyRelease(KeyEvent.VK_V);

       robot.keyRelease(KeyEvent.VK_CONTROL);

    }

         

posted @ 2017-08-23 17:26  爱吃橙子  阅读(470)  评论(0编辑  收藏  举报