11

I am new to selenium, I have a script that uploads a file to a server.

In the ide version sort of speak it uploads the file, but when I export test case as python 2 /unittest / webdriver it doesn't upload it..

It doesn't give me any errors, just doesn't upload it...

The python script is:

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

I searched for solutions but I haven't found any except integrating it with AutoIt or AutoHotKey...

The first line opens the File Upload Box of Firefox.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
user2782827
  • 121
  • 1
  • 1
  • 5
  • Is there more to the script? I would think that something like `driver.find_element_by_css_selector("input[type=\"file\"]").submit()` would be necessary, but I don't know exactly how file input boxes work with selenium. – Patrick Collins Sep 16 '13 at 12:35
  • Duplicate: https://stackoverflow.com/q/8665072/6003362 – Sahil Agarwal Oct 24 '17 at 11:21

5 Answers5

8

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")
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Men, ived tested every combination to link the file to the .send_keys(the file), it just don't work for me, is there a setting or something that i'm not aware of? – user2782827 Sep 16 '13 at 08:55
  • @user2782827, Which version of python/selenium do you use? Which OS/browser do you use? – falsetru Sep 16 '13 at 09:08
  • Yeah, sorry, i should have wrote that in the post, Windows7, python 2.7, firefox 22 or 23, selenium 2.3.0 – user2782827 Sep 16 '13 at 09:14
  • @user2782827, I experienced a problem while answering [this](http://stackoverflow.com/a/18696133/2225682) with lower version of selenium + firefox. Try to upgrade the selenium to recent version. I used *selenium 2.35.0* to test your code. (*ubuntu 12.10 64bit*, *python 2.7.3*) – falsetru Sep 16 '13 at 09:22
  • hmmmmmmmmmmmmmmmmm, k – user2782827 Sep 16 '13 at 09:23
  • Ok, did some updates, firefox 22.0, WinXP(not 7),selenium 2.35.0, python 2.7.5, still the same ugly thing, i forgot to mention that the upload is made by the File Upload box that firefox has.. – user2782827 Sep 16 '13 at 12:45
2

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")
vysakh
  • 266
  • 2
  • 4
  • 19
1

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.

oz123
  • 27,559
  • 27
  • 125
  • 187
user2782827
  • 121
  • 1
  • 1
  • 5
1

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

Using Pyautowin

from pywinauto import Desktop
driver.find_element_by_id("start-upload-button-single").click()
app = Desktop()['Open']   # About `Open`, checkout upload Dialog title
app.wait('visible')
app.Edit.type_keys(R"C:\Documents and Settings\pcname\Desktop\ffdlt\test.jpeg", with_spaces=True)
app['Open'].click()  # About `Open`, checkout upload Button name
Albertix
  • 11
  • 4