How to handle windows file upload window when using selenium

前端 未结 2 1101
时光取名叫无心
时光取名叫无心 2020-12-30 15:43

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

相关标签:
2条回答
  • 2020-12-30 15:57

    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.

    0 讨论(0)
  • 2020-12-30 15:59

    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);
    
    0 讨论(0)
提交回复
热议问题