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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

10 Comments

I have tried it exactly as you specified it, but the file upload dialogue just stays open, with no file being uploaded.
@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 try dropzonejs.com here is the commands on that site: def test_testdropzone(self): success = True wd = self.wd wd.get("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)
@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.
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?
|
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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.