java 使用selenium时如何处理windows文件上传窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16597588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle windows file upload window when using selenium
提问by Zoltorn
I am trying to write selenium tests for a website using java. However, I have come across a problem when testing file uploading..
我正在尝试使用 java 为网站编写 selenium 测试。但是,我在测试文件上传时遇到了一个问题。
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?
当我单击文件上传按钮时,它会自动打开 windows 文件上传。我有代码可以成功地将文本放入上传框中,只是我无法阻止 Windows 框自动出现,并且让网站不自动打开 Windows 文件上传并不是一个真正的选择。通过研究这个主题,我了解到 selenium webdriver 无法处理这个问题。所以我的问题是:有什么方法可以简单地以自动方式关闭上传窗口?
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)
我已经尝试过 java 机器人课程,但没有奏效。在执行我给它的任何命令(ALT-F4,单击 xy 位置等)之前,它一直等到上传窗口关闭
Thanks in advance
提前致谢
EDIT:
编辑:
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
采纳答案by Petr Jane?ek
You can do a nonblocking click by using either one of these:
您可以使用以下任一方法进行非阻塞点击:
The Advanced User Interactions API(JavaDocs)
WebElement element = driver.findElement(By.whatever("anything"));
new Actions(driver).click(element).perform();
or JavaScript:
或 JavaScript:
JavascriptExecutor js = (JavascriptExecutor)driver;
WebElement element = driver.findElement(By.whatever("anything"));
js.executeScript("arguments[0].click()", element);
回答by praneel
I have answered this for a similar question. There are other solutions provided for Upload - Like using AutoIT. But I personally would defer to interact with any OS specific dialogues. Interacting with OS specific dialogues would limit you to run the tests from a given environment.
我已经回答了这个类似的问题。还为上传提供了其他解决方案——比如使用 AutoIT。但我个人会推迟与任何特定于操作系统的对话进行交互。与操作系统特定的对话交互会限制您从给定环境运行测试。
Selenium webdriver java - upload file with phantomjs driver
Selenium webdriver java - 使用phantomjs驱动上传文件
Always identify & interact with elements of type "file" when uploads are concerned. This would solve your issue of pop ups.
涉及上传时,始终识别“文件”类型的元素并与之交互。这将解决您的弹出窗口问题。
Ex: In my application, upload related elements have the below DOM -
例如:在我的应用程序中,上传相关元素具有以下 DOM -
<a id="uploadFileButtonLink" class="uploadFileButtonLink" href="javascript:void(0)" data-uidsfdc="3" style="display: none;">Upload a file</a>
<input id="multiFileInput" class="multifile-upload-input-button" type="file" name="chatterFile_upload" multiple="multiple"/>
<input id="multiUploadBtn" class="btnImportant" type="button" value="Upload Files"/>
In this case, you can use sendKeys method to "multiFileInput" which is of type "file". This way it would work for all FF, Chrome & also headless browsers.
在这种情况下,您可以将 sendKeys 方法用于“文件”类型的“multiFileInput”。这样它就适用于所有 FF、Chrome 和无头浏览器。