【问题标题】:How to press ENTER key from Robot class?如何在机器人类中按 ENTER 键?
【发布时间】:2021-06-16 13:48:55
【问题描述】:

我创建了一种方法来从 Firefox 工具栏中单击特定插件,并且在 Windows 操作系统上一切正常。但是当我尝试从使用 Linux 机器的 Jenkins 服务器执行相同的操作时,它无法执行 robot.keyPress(KeyEvent.VK_Enter)。

    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_L);
    robot.keyRelease(KeyEvent.VK_L);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    Thread.sleep(2000);

    //move to addons
    Robot robot1 = new Robot();
    robot1.keyPress(KeyEvent.VK_TAB);
    robot1.keyRelease(KeyEvent.VK_TAB);
    Thread.sleep(1000);
    robot1.keyPress(KeyEvent.VK_TAB);
    robot1.keyRelease(KeyEvent.VK_TAB);
    Thread.sleep(1000);

    //move to simple-modify-header addon
    Robot robot3 = new Robot();
    robot3.keyPress(KeyEvent.VK_RIGHT);
    robot3.keyRelease(KeyEvent.VK_RIGHT);
    Thread.sleep(2000);
    robot3.keyPress(KeyEvent.VK_RIGHT);
    robot3.keyRelease(KeyEvent.VK_RIGHT);
    Thread.sleep(2000);
    robot3.keyPress(KeyEvent.VK_RIGHT);
    robot3.keyRelease(KeyEvent.VK_RIGHT);
    Thread.sleep(2000);
    robot3.keyPress(KeyEvent.VK_RIGHT);
    robot3.keyRelease(KeyEvent.VK_RIGHT);
    Thread.sleep(2000);


    //click simple-modify-header addon
    Robot robot2 = new Robot();
    robot2.keyPress(KeyEvent.VK_ENTER);
    robot2.keyRelease(KeyEvent.VK_ENTER);
    Thread.sleep(1000);

我尝试使用不同的机器人实例,但它仍然卡在按 ENTER 键。

【问题讨论】:

    标签: java selenium automation awtrobot


    【解决方案1】:

    这对我来说很好用:

    public static void robotDoEnter()
        {
            try {
                Robot robot = new Robot();
                robot.keyPress(KeyEvent.VK_ENTER);
                robot.delay(300);
                robot.keyRelease(KeyEvent.VK_ENTER);
                robot.delay(300);
            } catch (AWTException e) {
                e.printStackTrace();
            }
        }
    

    【讨论】:

    • 我尝试在 keyPress() 和 keyRelease() 行之间添加延迟,但我仍然遇到同样的问题。我无法在 linux 上按 ENTER 键。在 Windows 上一切正常。
    • 好吧,我只在 Windows 上使用过它,而且效果很好。没有在 Linux 或 Mac 上尝试过,抱歉。
    • 你必须使用机器人吗?行动将不是一种选择?
    • 我也用 Actions 尝试过,但仍然是同样的问题。我在帖子上添加了一个屏幕截图,使用机器人类,我可以转到 simple-modify-headers,然后当我想点击进入时,它会打开弹出窗口。
    猜你喜欢
    • 2020-09-07
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2010-12-08
    • 1970-01-01
    相关资源
    最近更新 更多