使用Python Selenium WebDriver上传多个文件

时间:2022-01-13 01:51:31

I have a html element

我有一个HTML元素

<input type=file multiple="">

How can I use send_keys to upload multiple files?

如何使用send_keys上传多个文件?

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

目前,这适用于上传单个文件。我想用它来上传多个文件

I tried Comma separated paths but no luck.

我尝试过逗号分隔的路径,但没有运气。

3 个解决方案

#1


0  

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

首先,将所有文件发送到元素,然后提交。

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

以下是Ruby代码,但您可以为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.

我不确定这是否有效,但只是尝试一下,让我知道结果。

#2


0  

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

我试过这个。 uploader.send_keys'path_to_file1'将上传file1,当我尝试上传file2时,

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

异常被抛出说“uploader”元素无法与之交互

I did a uploader.is_enabled()

我做了一个uploader.is_enabled()

It gives me false

它让我虚伪

#3


0  

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.

照片是一个字符串数组,即['/ Users/foo/bar/beautiful_forest-1546653.jpg','/ Users / foo / bar / DTHalloween.jpg']然后我循环并通过send_keys上传它们。我确保通过检查上传的文件名是否存在于DOM中来上传它们,如果成功的话,它将会存在(特定于我的情况)。 FWIW,我正在测试一个react.js网络应用程序顺便说一句。

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

#1


0  

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

首先,将所有文件发送到元素,然后提交。

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

以下是Ruby代码,但您可以为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.

我不确定这是否有效,但只是尝试一下,让我知道结果。

#2


0  

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

我试过这个。 uploader.send_keys'path_to_file1'将上传file1,当我尝试上传file2时,

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

异常被抛出说“uploader”元素无法与之交互

I did a uploader.is_enabled()

我做了一个uploader.is_enabled()

It gives me false

它让我虚伪

#3


0  

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.

照片是一个字符串数组,即['/ Users/foo/bar/beautiful_forest-1546653.jpg','/ Users / foo / bar / DTHalloween.jpg']然后我循环并通过send_keys上传它们。我确保通过检查上传的文件名是否存在于DOM中来上传它们,如果成功的话,它将会存在(特定于我的情况)。 FWIW,我正在测试一个react.js网络应用程序顺便说一句。

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