Selenium Close File Picker Dialog

瘦欲@ 提交于 2020-05-10 18:39:09

问题


We are using Selenium-Webdriver on Jenkins box (running linux), to drive Firefox for testing a fairly complex web app. The web app requires the uploading of a photo for testing, and we have achieved that by using sendkeys to the input file dialog. Unfortunately (perhaps due to the way the uploader works, it is plupload and uploads through XHR and not a FORM post) the File Picker Dialog never closes. While this was slightly annoying, in the past the tests still passed fine. Switching from selenium-2.45.0 to selenium-2.53.1 and upgrading FireFox to 45 we discovered that our tests now failed because element is no longer visible due to the File Picker Dialog (at least we assume that is reason that the elements no longer react to clicks is because they are behind the file dialog).

I realize that there is no way to click on the close dialog button, and I have tried sending the escape key various ways to browser to close the dialog but nothing I have tried works:

((RemoteWebDriver)driver).getKeyboard().pressKey(Keys.ESCAPE);

Since the Jenkins server running on the tests is on a different server than the Firefox being driven by Selenium, Roboto and AutoIt aren't an option. Is there some way to trigger closing of dialog that doesn't involve interacting with the dialog box. My thought was injecting some javascript into the page to create a form around the file input, and then submit the form (to nowhere), and then reloading the page but that has proven unsuccessful.


回答1:


There's probably a click event transmitted to the <input type="file"> element, which opens the file dialog.

One way to overcome this issue is to disable the internal click by overriding the method. This piece of JavaScript will disable the internal click for each <input type="file"> element:

((JavascriptExecutor)driver).executeScript(
    "HTMLInputElement.prototype.click = function() {                     " +
    "  if(this.type !== 'file') HTMLElement.prototype.click.call(this);  " +
    "};                                                                  " );

Then upload the file by calling .sendKeys("full file path") on the <input type="file"> element.




回答2:


  1. you can use sendKeys("\path\to\your\file') into input element without click nothing- sometimes it works.
  2. sometimes the dialog is popup so you can use: Alert e = driver.switchTo().alert()
  3. automate this dialog using coded ui \ autoit


来源:https://stackoverflow.com/questions/39259460/selenium-close-file-picker-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!