【问题标题】:python file upload seleniumpython文件上传硒
【发布时间】:2017-09-13 22:39:35
【问题描述】:

我使用 Selenium 和 Python 向网站发送请求。我现在正在上传文件,但它似乎没有像我预期的那样工作。

下面是 HTML 的样子:

<div id="uploaders" class="uploaders" data-image-urls="" data-image-ids="" data-image-positions="" data-image-checksums=""><div id="uploader-container-0" class="uploader-container small empty" data-uploader-index="0" data-numbering="1" style="position: relative;">
<div id="file-picker-0" class="uploader-box small" style="position: relative; z-index: 1;">
    <div class="thumb">
        <div class="uploader-overlay">
            <span class="photo-action edit-action">
                <span class="mp-Icon-circle"><span class="mp-Icon mp-svg-edit photo-action-icon"></span></span>
            </span>
            <span class="remove photo-action">
                <span class="mp-Icon-circle"><span class="mp-Icon mp-svg-delete photo-action-icon"></span></span>
            </span>
        </div>
    </div>
    <div class="content">
        <div class="mp-svg-button-camera camera-logo centered"></div>
        <div class="main-photo-subtext centered">
            Hoofdfoto.
        </div>
    </div>
</div>


<input type="hidden" name="images.urls" value="">
<input type="hidden" name="images.ids" value="">
<input type="hidden" name="images.checksums" value="">
<div id="html5_1bdvv7bg0ptl15m6gmu1d4v16l04_container" class="moxie-shim moxie-shim-html5" style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; overflow: hidden; z-index: 0;">
<input id="html5_1bdvv7bg0ptl15m6gmu1d4v16l04" type="file" style="font-size: 999px; opacity: 0; position: absolute; top: 0px; left: 0px; width: 100%; height: 100%;" multiple="" accept="image/jpeg,.jpg,.jpeg,image/bmp,.bmp,image/png,.png"></div>

正如您在代码底部看到的,有一个输入类型文件元素。但没有形式。网站上的文件上传工作如下。您单击 (+) 符号。然后我文件选择对话框打开,你选择文件。然后会出现另一个弹出窗口,要求您点击一个按钮是否要接受某些内容。

我的问题是如何上传文件。到目前为止,我还没有做到这一点。

我的代码:

upload_photo_field_xpath = ".//*[@id='uploader-container-0']/div/input"
upload_photo_element = WebDriverWait(driver, 10).until(
                                        lambda driver: driver.find_element_by_xpath(upload_photo_field_xpath))

image = os.path.join(os.getcwd(), 'images/' + 'img.jpg')
print(image)
upload_photo_element.send_keys(image)
upload_photo_element.submit()

关于如何解决这个问题的任何想法?

谢谢。

【问题讨论】:

    标签: python file selenium upload


    【解决方案1】:

    您可以根据您的要求尝试以下步骤,这对我来说与上传文件相关

    安装win32com.client

    pip install pypiwin32
    

    然后将其包含在您的代码中,如下所示

    import win32com.client
    
    //click on upload button for uploading file
    time.sleep(3)//you can use explicit wait here
    shell = win32com.client.Dispatch("WScript.Shell")
    shell.Sendkeys("C:\\Users\\.....\\File.pdf")//file path to be uploaded
    time.sleep(2)
    shell.Sendkeys("{ENTER}")
    time.sleep(2)
    

    【讨论】:

    【解决方案2】:

    没关系。我只是通过从浏览器切换来测试这个。我没有使用 PhantomJS,而是使用 Firefox 进行测试。所以我可以查看幕后实际发生的事情。通过查看浏览器,我了解到处理文件上传处理需要等待几秒钟。

    代码:

    '''Image Upload'''
    
    upload_photo_element = driver.find_element_by_xpath(upload_photo_field_ID)
    image = os.path.join(os.getcwd(), 'images/' + 'img.png')
    upload_photo_element.send_keys(image)
    
    # Depending on how long the image takes to load
    time.sleep(5)
    
    upload_photo_reject_button_element = WebDriverWait(driver, 10).until(
    lambda driver: driver.find_element_by_id(upload_photo_reject_button_ID))
    
    upload_photo_reject_button_element.click()
    
    '''End image upload'''
    

    此代码对我有用。

    这样就不会弹出窗口了。

    但我也注意到这适用于 Firefox 浏览器,但不适用于 PhantomJS。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-05
      • 2016-10-22
      • 2018-07-07
      相关资源
      最近更新 更多