简体   繁体   English

如何使用python + selenium上传文件?

[英]How to upload file using python+selenium?

I need to upload file. 我需要上传文件。 I have "Choose file" form. 我有“选择文件”表格。 I click "Choose file" button, select file in window, after that uploading starts. 我点击“选择文件”按钮,在窗口中选择文件,然后开始上传。

Here is form. 这是表格。

<form id="fileupload" method="POST" enctype="multipart/form-data" data-confirm="true">
<div class="uploadForm">
<div class="browseUploadLeft">
<i class="icon icon-discontinued-upload"></i>
<h3>Browse and choose</h3>
<p>files from your computer</p>
</div>
<div class="browseUploadRight">
<p class="browseInfo is-hidden">Need help? See <span>Upload Rules</span></p>
<span class="button button-pink buttonFullWidth">choose files to upload
<input type="file" id="fileUploadField" name="Filedata" multiple class="filemultiple video" />
</span>
<p class="sizeNote">Maximum file size limit is 10GB</p>
</div>
</div>
<input type="hidden" name="userId" value="8996317" />
<input type="hidden" name="videoId" class='be_video_id' id='be_video_id' value="" />
<input type="hidden" id="_token" name="_token" value="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJodHRwOlwvXC93d3cueW91cG9ybi5jb20lIiwic3ViIjoiODk5NjMxNyIsImF1ZCI6Imh0dHA6XC9cL3d3dy55b3Vwb3JuLmNvbSUiLCJpYXQiOjE1MDkzNTI5MjEsImV4cCI6MTUwOTM4ODkyMX0.DSMPckUG3ZcL6Zrbn1WWrLgLzJ_tdw3TOZ2hnb2z60qSWMMpfZghJnrliSkwAQVRNjl6H-VKDCZBrlKvGwO0WQ" />
</form>

I want do this using python + selenium. 我想使用python +硒做到这一点。 I have code. 我有代码。

browser = webdriver.Firefox()
browser.get("url") 
time.sleep(10)
browser.find_element_by_id("fileUploadField").click()
file = browser.find_element_by_id("fileUploadField")
file.send_keys("1.m4")
file.submit()

But I have error during execution the code. 但是我在执行代码时出错。

      File "yp.py", line 40, in <module>
        file.submit()
......

       Message: Element is no longer attached to the DOM

How can I send file to this form in correct way? 如何以正确的方式将文件发送到此表单? How to start upload file? 如何开始上传文件?

First install win32com.client. 首先安装win32com.client。 To install win32com.client type in cmd 要安装win32com.client,请在cmd中键入

pip install pypiwin32

Now after click on upload, add below code to pass address of file which need to be uploaded. 现在点击上传后,添加以下代码以传递需要上传的文件的地址。

browser = webdriver.Firefox()
browser.get("url") 
time.sleep(10)
browser.find_element_by_id("fileUploadField").click()
shell = win32com.client.Dispatch("WScript.Shell")
shell.Sendkeys("D:\\FileLocation\\1.m4")
shell.Sendkeys("{ENTER}")

Note: In case you get any issue while installing win32com.client then check whether you have installed python for 32 bit os, if not then make sure you have installed python for 32 bit even if your os is 64 bit os. 注意:如果在安装win32com.client时遇到任何问题,请检查是否已为32位操作系统安装了python,否则请确保已为32位操作系统安装了python(即使您的操作系统是64位操作系统)。 There won't be any problem in installation and win32com.client will work there 安装不会有任何问题,win32com.client将在那里工作

尝试设置完整路径:

driver.find_element_by_id("fileUploadField").send_keys(os.getcwd()+"/1.m4")

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

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