4

I have a html element

<input type=file multiple="">

How can I use send_keys to upload multiple files?

Currently this works with uploading single file. I want to use this to upload multiple files

I tried Comma separated paths but no luck.

1
  • Separate with '\n', not with comma. Commented May 31, 2022 at 16:55

6 Answers 6

3

path = “/home/downloads/” send_keys(path + “file1.csv \n” + path + “file2.csv”)

I found it working in my code. Do try this and give me update on the error you got.

Sign up to request clarification or add additional context in comments.

Comments

1

First, send all files to the element and then submit.

Following is Ruby code, but you can apply same logic for Python:

uploader = driver.find_element(id: 'file-upload')
uploader.send_keys 'path_to_file1'
uploader.send_keys 'path_to_file2'
uploader.send_keys 'path_to_file3'
.
.
.
uploader.submit

I'm not sure if this will work, but just give a try and let me know the result.

Comments

1

Here is an example that works in my specific case for uploading multiple photos, may help someone out...

Photos is an array of strings i.e. ['/Users/foo/bar/beautiful_forest-1546653.jpg', '/Users/foo/bar/DTHalloween.jpg'] then I loop through and upload them by send_keys. I ensure they are uploaded by checking to see if the uploaded filename exists in the DOM, which it will if successfull (specific to my case). FWIW, I'm testing a react.js web app btw.

def uploadPhoto(self, photos):
    try:
        drop_zone = self.driver.find_element_by_id('photo-file-input')

        alreadyUploaded = []  # keep track of uploaded files

        for photo in photos:
            photo_name = photo.split('/')[-1].split('.')[0]

            if photo_name.lower() in alreadyUploaded:
              print("Photo already uploaded with name: ( "+ photo_name.lower()+" )")
              continue

            alreadyUploaded.append(photo_name.lower())

            drop_zone.send_keys(photo)

            try:
                WebDriverWait(self.driver, 5).until(
                    EC.presence_of_element_located((By.XPATH, '//img[contains(@data-galleryid, '+ photo_name +')]'))
                )
            except Exception, e:
                raise Exception(e)
        return True
    except Exception, e:
        print 'Failed to upload photo {}'.format(str(e))
        return False

3 Comments

I think this is some kind of buggy scenario, because there are two requests with single file instead of one with multiple files. In my case one request starts big machinery for all files. Do you know how to do it with one request?
@Netrix Not sure if your using the actual code I supplied but it does not have logic to determine if a photo with a specific name has already been uploaded, so hence why it would produce duplicates. I have edited my post to include such logic. Hopefully that answers your question.
Thanks, but what I found out is that one call of send_keys produce one POST in my case. What I want is to be able to upload multiple files wit hone send_keys request. I found out how to do it but it probably uses a bug that keeps previously sent file for next request so when I call send_keys for second file it will actually send two files.
0

I tried this. uploader.send_keys 'path_to_file1'will upload file1 and when i try to upload file2,

exception is thrown saying "uploader" element cannot be interacted with

I did a uploader.is_enabled()

It gives me false

Comments

0

I didn't test this code, but I think it should work

image_string = " ".join(images_array)
driver.find_element_by_class_name('PhotoInputFile').send_keys(image_string)
time.sleep(2)
driver.find_element_by_class_name("ButtonUploadPhotos").click()

Comments

0

Try using this because for me works :

 @staticmethod
 def set_multiple_file_file():
   element = (INSERT_FILE)
   ROOT_DIR = your.path
   first_path = archive1.something
   second_path = archive2.something
   third_path = archive3.something
   element.send_keys(ROOT_DIR + first_path + ROOT_DIR + second_path + ROOT_DIR + third_path)

1 Comment

Code-only answers are not particularly helpful. Please add some descriptions of how this code solves the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.