机器人类-KeyEvent无法将文档上传到KeyEvent.VK_V处的Windows文件夹中

问题描述 投票:0回答:1

我正在特定情况下使用硒上传文档。反复失败后,我认为自己要调试代码。这是我的代码和错误。

public void uploadFile(String imagePath) {
        StringSelection stringSelection = new StringSelection(imagePath);
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(stringSelection, null);

        Robot robot = null;

        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }

        robot.delay(250);

        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);

调试时,我发现了以下结果。

robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
The below happens where the selected path gets printed in the code right next to Vk.Control
  C:\SAF\GIT\TestImage\Tests.docx       robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);

请让我知道是否有任何解决方法?

java eclipse selenium webdriver robot
1个回答
0
投票

保持窗口最大化,

  before robot.delay(250);
  driver.manage().window().maximize();

[如果需要,请在键盘事件后添加一些延迟,如下所示

    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.delay(50); // it will slow down keyevent renderence and synchronize.
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
© www.soinside.com 2019 - 2024. All rights reserved.