简体   繁体   中英

How to handle windows file upload window when using selenium

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

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?

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 xy position, etc)

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

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:

JavascriptExecutor js = (JavascriptExecutor)driver;

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

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.

Selenium webdriver java - upload file with phantomjs driver

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 -

<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM