selenium webdriver upload file

南笙酒味 提交于 2019-11-27 09:16:16

Your code work perfectly for me (I test it with Firefox, Chrome driver)

One thing I supect is excessive backslash(\) escape.

Try following:

driver.find_element_by_id("start-upload-button-single").click()
driver.find_element_by_css_selector('input[type="file"]').clear()
driver.find_element_by_css_selector('input[type="file"]').send_keys("C:\\Documents and Settings\\pcname\\Desktop\\ffdlt\\test.jpeg")

or

driver.find_element_by_id("start-upload-button-single").click()
driver.find_element_by_css_selector('input[type="file"]').clear()
driver.find_element_by_css_selector('input[type="file"]').send_keys(r"C:\Documents and Settings\pcname\Desktop\ffdlt\test.jpeg")

Did you tried this single piece of code:

driver.find_element_by_css_selector("input[type=\"file\"]").send_keys("C:\\Documents and Settings\\pcname\\Desktop\\ffdlt\\test.jpeg")
user2782827

If I run the following lines from the IDE it works just fine, it uploads the file.

Command | Target                               | Value
_____________________________________________________________
open    | /upload                              |
click   | id=start-upload-button-single        |
type    | css=input[type="file"]               | C:\\Documents and Settings\\cristian\\Desktop\\ffdl\\MyWork.avi

But when I export it for Python webdriver it just doesn't upload it, I have tried everything.

The last resort is to make it work with AutoHotKey, but I want it to work.

What I have done is tested the solutions that I have found with/on other sites to see if the problem is only on the site that i am trying to make the upload(youtube), the solutions work(EX: http://dev.sencha.com/deploy/ext-4.0.0/examples/form/file-upload.html) they are valid, you can upload a file to most servers, it just doesn't work on it.

Thank you for your help.

This works for me:

# Upload file
elem = driver.find_element_by_name("File")
elem.send_keys(r"D:\test\testfile04.txt")
elem = driver.find_element_by_partial_link_text("Upload File")
elem.click()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!