0

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

2 Answers2

1

First install win32com.client. To install win32com.client type in 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. There won't be any problem in installation and win32com.client will work there

Shoaib Akhtar
  • 1,393
  • 5
  • 17
  • 46
0

Try setting full path:

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