Selenium WebDriver: Upload multiple files

15,447

Solution 1

As far as I know, selenium still does not support multiple file upload (see issue on google code).

There is at least one workaround: apparently create a form that contains as many input fields as you need (see another stackoverflow question). Not the best solution, as it (probably) requires altering your code for selenium to work.

However, as you have found out (thanks for this!), it does seem possible to trigger multiple file uploads in chrome and (although I did not test it) IE as well.

I just confirmed that the chrome "\n" trick works both locally and on Browserstack (I used the default images they provide), which, considering the state of things, is good enough for me.

I hope this helps.

Solution 2

The solution for me (selenium in python) was to just repeat send_keys for each image path before uploading.

Example for two files:

driver.find_element_by_name("filename").send_keys(file_path_1)
driver.find_element_by_name("filename").send_keys(file_path_2)
driver.find_elements_by_xpath("//*[contains(text(), 'Upload')]")[0].send_keys(Keys.RETURN)
Share:
15,447

Related videos on Youtube

user1944151
Author by

user1944151

Updated on October 26, 2022

Comments

  • user1944151
    user1944151 over 1 year

    My test need to upload test files in different browsers (I use WebDriver + Java). For a single file upload, everything works fine. I just send the path

    "C:\\testdata\\testfile.txt"
    But, syntax changes for multiple upload and different browsers.
    
    (
    IE: 
    "\"" + "C:\\Selenium\\TestData\\Flexy - BigFile1.txt"+"\"" +"\""+"C:\\Selenium\\TestData\\Flexy - BigFile2.txt" + "\""
    
    CHROME: 
    "C:\\Selenium\\TestData\\Flexy - BigFile1.txt"+"\n"+"C:\\Selenium\\TestData\\Flexy - BigFile2.txt".
    

    Firefox: I'm not able to find a correct syntax.

    Any idea?

    Is there a common syntax for all browsers?

  • Justin Force
    Justin Force about 8 years
    Some feedback to help you improve the quality of your posts: I don't know what AutoIT is; I can't tell what language the code snippet is supposed to be in or what the context is supposed to be; and the code sample is full of "pretty" quotes and is not formatted.
  • samuraiseoul
    samuraiseoul over 7 years
    Awesome! I would have never realized that end of line was the solution if not for your answer. Works for georgesironi selenium test in php! Thanks for this.
  • Joseph238
    Joseph238 about 3 years
    Looks like the "\n" solution now works in Firefox as well.
  • Joseph238
    Joseph238 about 3 years
    Folder upload is supported too (also using SendKeys), but only for Chrome (it's mentioned in this GitHub issue). I'm testing using Python Selenium with Chrome 89. Unfortunately doesn't seem to work for Firefox. I haven't tried other browsers.
  • Aaron Franke
    Aaron Franke about 2 years
    Is there a way to upload files if a website does not contain a <input type="file"> element and instead uses a custom upload button? Is there a way that I can control the file dialog provided by the OS and browser?