Python 3、Selenium、Os - 如何上传文件夹中的所有图像

马特威尔逊

我正在制作一个应用程序(Windows 10 和 selenium chromedriver),它在广告网站上为我制作广告,包含所有详细信息,现在我想为它上传图片的部分。我正在尝试类似的东西:

# Upload images
# Change the directory from the current running project's directory
# to the subsequent images folder
os.chdir(os.getcwd()+"/images")

# For each image that's in the folder
for file in os.getcwd():

# Click the upload button on the website to open up the windows 
# directory viewer, and then select all the images it sees there?
    driver.find_element_by_xpath('//*[@id="ImageUploadButton"]').send_keys(os.getcwd()+file)

# change directory back to the original os.getcwd()
os.chdir("..")

...试图适应我在 SO 上找到的另一个答案。我有程序运行到这一点,它运行没有错误,但是它通过打开这个目录窗口来完成:

在此处输入图片说明

...这显然不是项目的目录,也不是项目/图像文件夹的目录。我什至不确定这是正确的解决方案,但这是我第一次尝试这样做。有任何想法吗?谢谢!

更新

根据要求,这里是文件上传部分的 html 代码:

<h2>
                <div class="number">4</div>
Media</h2>
        <ul class="post-ad-layout">
                <li class="jsonly">
                <div id="MediaImageUpload" class="clearfix form-section placeholders">
                    <p class="images-title">Add at least one photo to complete your ad.</p>
                    <div class="images-content">
                        <h3>Add photos to attract interest to your ad</h3>
                        <div class="images-content-secondary">
                            <p>Include pictures with different angles and details. You can upload a maximum of <span id="MaxImages">10</span> photos, that are at least 300px wide or tall (we recommend at least 1000px).</p>
                            <p>Drag and drop to change the order of your pictures.</p>
                        </div>
                    </div>
                    <ol id="MediaUploadedImages">
                        </ol>

                    <span class="field-message" data-for="FileUploadInput"></span>

                    <div id="FileInputWrapper" class="file-input-wrapper">
                        <input type="hidden" name="file" class="fileErrorBox">

                        <div class="imageUploadButtonWrapper">
                            <button id="ImageUploadButton" type="button" class="button-update-cancel short file-upload-button">
                                Select Images</button>
                        </div>
                    </div>
                </div>
            </li>
化脓

不在服务器上本地测试。

# get the button element
ele = driver.find_element_by_id("ImageUploadButton")
# add a hidden file input ( might have to change the onchange event based on the events associated to the button in above line as you don't have a form)
driver.execute_script("var x=  document.createElement('INPUT');x.setAttribute('type', 'file'); x.setAttribute('onchange','this.form.submit()');x.setAttribute('hidden', 'true'); arguments[0].appendChild(x);",ele)
# send the picture path here ( this should upload the file)
driver.find_element_by_xpath("//input[@type='file']").send_keys("picture path should go here")

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用python和os合并特定文件夹中的所有csv文件

如何使用python按名称加载文件夹中的所有图像?

捕获主目录中所有子文件夹中的所有csv文件-Python 3.x

如何从文件夹中读取所有图像并将同名图像保存在其他文件夹python中

如何使用os.system使用外部python脚本一次转换文件夹中的所有文件

如何使用python删除文件夹中的所有文件夹?

python脚本获取多个子文件夹中的所有.mp3文件并移动到PC上的一个文件夹?

如何在AWS Lambda(python)中上传zip文件夹而不存储在S3中

如何使用 Google Drive API v3 将可恢复上传到 Python 中的共享文件夹?

如何使用 python 3.8 将文件上传到 AWS S3 子文件夹?

Python / OpenCV-如何按字母顺序从文件夹加载所有图像

如何检查python文件夹中的所有文件是否具有不同的内容?

Python:解析文件夹中的所有文件

Python映射文件夹中的所有文件

打开文件夹python中的所有文件

遍历Python文件夹中的所有文件

python重命名文件夹中的所有文件

如何使用Python连接文件夹中的所有csv文件?

如何从Python中的不同文件夹导入所有文件

如何查找使用Python在特定文件夹中今天创建的所有文件

如何使用python代码处理文件夹中的所有文件

如何使用python遍历多个文件夹中的所有文件

Python:如何使用具有写入权限的“os.mkdir()”在 Windows 中创建文件夹?

如何授予对Amazon S3中文件夹的所有子文件夹的访问权限?

如何在OS X上的文件夹内的所有.php文件中替换字符?

如何从Amazon S3存储桶中的文件夹中获取所有文件?

将JSON文件上传到Python中s3存储桶中的特定文件夹

Python:将目录中的所有文件夹压缩

如何使用Python Google Colab将带有边框的图像存储在文件夹中?