2

I am attempting to upload a file using selenium web driver. I get the file upload dialogue box to open in both MacOS and Windows, after which nothing happens. Wondering why selenium does not open the file via the upload dialog?

Webdriver commands I am using:

wd.get("http://www.dropzonejs.com/")
wd.find_element_by_css_selector("div.dz-message").click()
wd.find_element_by_css_selector("input.dz-hidden-input").click()
elm = wd.find_element_by_xpath("//input[@type='file']")
elm.send_keys("/Users/bg/Downloads/YOURFILE.PDF")
elm.submit()
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Barry G
  • 221
  • 4
  • 14
  • Possible duplicate of [selenium webdriver upload file](http://stackoverflow.com/questions/18823139/selenium-webdriver-upload-file) – jb. Mar 01 '16 at 11:15

2 Answers2

7

Don't click the file input element - it would trigger a file upload dialog which you cannot control via selenium. Send the keys to the input and submit the form:

elm = wd.find_element_by_xpath("//input[@type='file']")
elm.send_keys("/Users/bg/Downloads/myfile.PDF")
elm.submit()

submit() in this case is called on an input element - selenium would find the corresponding to the input element form and submit it.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • I have tried it exactly as you specified it, but the file upload dialogue just stays open, with no file being uploaded. – Barry G May 13 '15 at 16:42
  • @BarryG I don't see anything here that usually triggers the file upload dialog. Is this a public site and I can try reproducing the issue too? Thanks. – alecxe May 13 '15 at 21:28
  • @ alecxe try http://www.dropzonejs.com/ here is the commands on that site: def test_testdropzone(self): success = True wd = self.wd wd.get("http://www.dropzonejs.com/") wd.find_element_by_css_selector("div.dz-message").click() wd.find_element_by_css_selector("input.dz-hidden-input").click() elm = wd.find_element_by_xpath("//input[@type='file']") elm.send_keys("/Users/bg/Downloads/YOURFILE.PDF") elm.submit() self.assertTrue(success) – Barry G May 14 '15 at 17:59
  • @BarryG that's what I'm talking about. In your code, `wd.find_element_by_css_selector("div.dz-message").click()` triggers the file upload dialog - you have to avoid it. – alecxe May 14 '15 at 22:14
  • I removed that line, however now the file upload does not trigger nor does the dialogue box open. What would the selenium commands look like in order to do a file upload on www.dropzonejs.com? – Barry G May 15 '15 at 16:24
  • @BarryG well, if the only thing you are doing is locating the `input` and sending keys to it with path to a file - there won't be any dialog box open and this is how it has to work - you have to avoid the file open dialog being opened. Here is the code I'm using https://gist.github.com/alecxe/cacd306e58e143bc57de. After executing it I see the filename in the upload box.. – alecxe May 15 '15 at 21:38
  • Seems nearly identical to what I am doing, except with the firefox driver. Do you think its a problem with the firefox driver? – Barry G May 18 '15 at 15:36
  • @BarryG yeah, I see it works differently in firefox. Are you okay to just switch to chrome? – alecxe May 18 '15 at 18:39
  • problem is that chrome is not default driver and we try to run tests using remote machines as well, which using chrome with selenium server is more complicated. – Barry G May 18 '15 at 20:03
  • tried it with chrome here is the error: selenium.common.exceptions.ElementNotVisibleException: Message: element not visible (Session info: chrome=43.0.2357.130) (Driver info: chromedriver=2.15.322455 (ae8db840dac8d0c453355d3d922c91adfb61df8f),platform=Mac OS X 10.10.2 x86_64) – Barry G Jun 22 '15 at 21:45
0

I finally found the code I was looking for to solve my problem. I'm going with 2 hours of research to find a solution to my problem. In my case I needed to send an image of my pc to a program through python. The page only has 1 button to upload the photo and one to send. Thank you very much for having made the code available

exemple of a program python:

from selenium import webdriver
browser=webdriver.Chrome()
browser.maximize_window()
browser.get(('http://127.0.0.1/namepage.exp'))
elm = browser.find_element_by_xpath('//*[@id="exp_file"]') #
elm.send_keys("C:\PycharmProjects\\varios\image.png")
elm.submit()
DAVID
  • 1