简体   繁体   中英

Upload multiple files using Python Selenium WebDriver

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.

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.

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

Photos is an array of strings ie ['/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

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.

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

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()

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM