简体   繁体   English

python文件上传硒

[英]python file upload selenium

I use Selenium and Python to send requests to a website. 我使用Selenium和Python将请求发送到网站。 I am making a file upload right now, but it seems that it is not working as I expect it to be. 我现在正在上传文件,但似乎无法正常工作。

Here is how to HTML looks like: 这是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>

As you can see in the bottom of the code there is a input type file element. 如您在代码底部所看到的,有一个输入类型文件元素。 But no form. 但是没有形式。 The file upload on the site works as follows. 在站点上载文件的工作方式如下。 You click on a (+) sign. 您单击(+)号。 Then I file choose dialog box opens, you choose the file. 然后我打开文件选择对话框,选择文件。 And then there comes another pop-up which asks with a button to click if you want accept something. 然后是另一个弹出窗口,要求您单击一个按钮以单击是否要接受某些内容。

My question is how can I upload a file. 我的问题是如何上传文件。 Till yet I have failed to do this. 到目前为止,我还没有做到这一点。

My code: 我的代码:

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

Any thoughts on how to solve this problem? 关于如何解决这个问题有什么想法?

Thanks. 谢谢。

You can try the below step as per your requirement which is working well for me related to upload file 您可以根据自己的要求尝试以下步骤,这对我来说与上传文件有关的效果很好

Install win32com.client 安装win32com.client

pip install pypiwin32

Then include it in your code as below 然后将其包含在您的代码中,如下所示

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)

Never mind. 没关系。 I just tested this by switching from browser. 我刚刚通过从浏览器切换来进行了测试。 Instead of using PhantomJS I used Firefox for testing purpose. 我没有使用PhantomJS,而是使用Firefox进行测试。 So I can view what actually is happening behind the scenes. 因此,我可以查看幕后实际发生的情况。 By viewing the browser I came to understand that it needed a couple of seconds to wait in order to handle the file upload handling. 通过查看浏览器,我了解到它需要等待几秒钟才能处理文件上传处理。

The code: 编码:

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

This code works for me. 该代码对我有用。

In this way no window will pop-up. 这样就不会弹出任何窗口。

But I noticed also that this works with the Firefox browser, but not with PhantomJS. 但我也注意到,这适用于Firefox浏览器,但不适用于PhantomJS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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