gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:26:12 26 4
gpt4 key购买 nike

我正在处理一个特定的场景,使用 selenium 上传文档。多次失败后我想自己调试一下代码。这是我的代码和错误。

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);

请告诉我是否有任何解决办法?

最佳答案

保持窗口最大化,

  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);

关于java - 机器人类 - KeyEvent 无法将文档上传到 Windows 文件夹 KeyEvent.VK_V,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62140516/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com