0

I am using Selenium WebDriver and Java to upload two files in a <input type='file'> tag.

I have adopted the way mentioned in this post.

This is my code snippet, to be specific.

wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(fileSelectionBox))).sendKeys
                (System.getProperty("user.dir") + invoiceFilePath + fileNames[0]
                        + "\n"
                        + System.getProperty("user.dir") + invoiceFilePath + fileNames[1]);

The problem is, although the two files are successfully uploaded, and application flow moves forward, I am still getting this exception from the code snippet mentioned above.

org.openqa.selenium.InvalidArgumentException: One or more files could not be selected.

The DOM looks like this.

<span class="w-upload-file__pseudo-button-span relative ">Choose Files</span>
    <div class="w-u-f__abs-button-wrapper "><span class="w-upload-file__pseudo-button-span relative ">Choose Files</span></div>
    <span class="w-upload-file__span" type="text" disabled="">No file selected</span>
    <span class="w-upload-file__input-focus-wrapper"></span>
    <div class="w-upload-file__dropzone-container" tabindex="0" aria-disabled="false"><input type="file" multiple="" autocomplete="off" style="display: none;"></div>
    <input type="file" multiple="" autocomplete="off" style="display: none;">
    <input type="button" pseudo="-webkit-file-upload-button" value="Choose Files">
    <input type="file" multiple="" autocomplete="off" style="display: none;">
    <div class="w-upload-file__dropzone-container" tabindex="0" aria-disabled="false"><input type="file" multiple="" autocomplete="off" style="display: none;"></div>

Xpath of "fileSelectionBox" is "//span[contains(text(), 'No file selected')]/preceding::input[@type='file']"

I need to get rid of this exception. Any idea on this?

  • Can you try to upload them separately? Upload the first one and then the second one in different lines of code. Maybe between them a time.sleep(2) too? I am not saying this is the solution but can you try it? – dpapadopoulos May 05 '20 at 14:50
  • Well, I had tried that. But the problem with that is the moment I upload one file, the application processes it, and throws error (valid application level error). I mean, the app allows only single attempt upload. – Nilanjan Guha Majumder May 06 '20 at 15:21
  • Hmm interesting and weird at the same time. Let me make research if this case was occurred by others too. – dpapadopoulos May 06 '20 at 17:36
  • However, as a note, if I upload one file, there is no code level exception. So, may be the way I am separating the two files in my code before sending those, may be the root of the problem? :) – Nilanjan Guha Majumder May 07 '20 at 11:33
  • I had the same problem, but since the files do get uploaded, I just catch the exception and move on without doing anything about it. Is that not an option for you? – Mr.O Oct 27 '21 at 13:04

0 Answers0