使用selenium时如何处理Windows文件上传窗口 [英] How to handle windows file upload window when using selenium

查看:2240
本文介绍了使用selenium时如何处理Windows文件上传窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to write selenium tests for a website using java. However, I have come across a problem when testing file uploading..

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

When I click the file upload button, it automatically opens the windows file upload. I have code working to put the text in the upload box successfully, it's just there is nothing I can do to stop the windows box from coming up automatically, and having the website not automatically open the windows file upload isn't really an option. From researching this subject I understand there is no way for selenium webdriver to handle this. So my question is this: what is a way I can simply close the upload window in an automated way?

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

I have tried the java robot class and it did not work. It waited until the upload window was closed before doing any of the commands I gave it (ALT-F4, clicking in an x-y position, etc)

提前致谢

编辑:

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


推荐答案

您可以使用以下任一方法执行非阻止点击:

You can do a nonblocking click by using either one of these:

高级用户交互API JavaDocs

WebElement element = driver.findElement(By.whatever("anything"));
new Actions(driver).click(element).perform();

或JavaScript:

or JavaScript:

JavascriptExecutor js = (JavascriptExecutor)driver;

WebElement element = driver.findElement(By.whatever("anything"));
js.executeScript("arguments[0].click()", element);

这篇关于使用selenium时如何处理Windows文件上传窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆