首页 > 解决方案 > 使用 Selenium-Python 通过 WebDriver 上传图像

问题描述

所以我正在制作这个 python 机器人来为 1 个转售网站自动发布广告。

而且我碰壁了,机器人登录做的一切都很完美,但是当涉及到图像上传时,我无法让它工作。

这是我用于图片上传的部分:

img_path = '/home/djurovic/Desktop/test/image.png'
imageUploadXpath = '//button[@id="ImageUploadButton"]'
imageUploadElement = WebDriverWait(browser, timeout).until(lambda browser: browser.find_element_by_xpath(imageUploadXpath))
imageUploadElement.send_keys(img_path)

整个脚本有很多代码,所以我不想在这个问题上过头,这是唯一困扰我的部分......

这是我试图自动化的网站:https ://www.kijiji.ca/

这就是网站的图片上传部分的样子 ->

在此处输入图像描述

这是图片上传部分的 html 端:

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

标签: pythonseleniumselenium-webdriverwebdriver

解决方案


推荐阅读