Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium WebDriver: Upload multiple files

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?

like image 642
user1944151 Avatar asked May 30 '14 13:05

user1944151


People also ask

Can we upload file using Selenium WebDriver?

We can upload files using Selenium Webdriver. This is achieved by the sendKeys method. We have to first identify the element which performs the file selection by mentioning the file path [to be uploaded].

How do you do file upload in WebDriver?

Uploading files in WebDriver is done by simply using the sendKeys() method on the file-select input field to enter the path to the file to be uploaded. WebDriver cannot automate downloading of files on its own.


2 Answers

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.

like image 156
L0LN1NJ4 Avatar answered Sep 22 '22 04:09

L0LN1NJ4


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)
like image 24
TRogers Avatar answered Sep 19 '22 04:09

TRogers