小编Zol*_*orn的帖子

使用selenium时如何处理Windows文件上传窗口

我正在尝试使用java为网站编写selenium测试.但是,我在测试文件上传时遇到了一个问题.

当我单击文件上传按钮时,它会自动打开Windows文件上传.我有代码工作将文本成功地放在上传框中,它只是没有什么我可以做的阻止Windows框自动出现,并且让网站不自动打开Windows文件上传不是一个真正的选择.通过研究这个主题,我知道selenium webdriver无法处理这个问题.所以我的问题是:我可以通过自动方式关闭上传窗口的方式是什么?

我已经尝试过java机器人类,但它没有用.它等到上传窗口关闭之后再执行我给它的任何命令(ALT-F4,点击xy位置等)

提前致谢

编辑:

wait.until(ExpectedConditions.elementToBeClickable(By.id(("addResourcesButton"))));
driver.findElement(By.id("addResourcesButton")).click();

//popup window comes up automatically at this point


try {
    Robot robot = new Robot();
    robot.mouseMove(875, 625);
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
} catch (AWTException e) {
    e.printStackTrace();
}

//my attempt to move the mouse and click, doesn't move or click until after I close the windows upload box

String fileToUpload = "C:\\file.png";


WebElement uploadElement = driver.findElement(By.id("fileInput"));
uploadElement.sendKeys(fileToUpload);

//Takes the code and successfully submits it to the text area, where I can now upload it
Run Code Online (Sandbox Code Playgroud)

java selenium webdriver

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

java ×1

selenium ×1

webdriver ×1