Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selenium webdriver upload file

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.

like image 730
user2782827 Avatar asked Sep 16 '13 08:09

user2782827


People also ask

Can we upload file using Selenium WebDriver?

We can upload files using Selenium Webdriver. This is achieved by the sendKeys method. We have to first identify the element which performs the file selection by mentioning the file path [to be uploaded].

How do you do file upload in WebDriver?

Uploading files in WebDriver is done by simply using the sendKeys() method on the file-select input field to enter the path to the file to be uploaded. WebDriver cannot automate downloading of files on its own.

How do I automate a file to upload?

To automate this process, there are 2 ways to fill the file upload control: If standard file dialog is used by the web page, upload action can be automated with pure selenium-webdriver. You can call sendKeys() on the file control to fill the file control with file path and then submit form.

Does Selenium support upload facility?

The most basic way of uploading files in Selenium is using the sendKeys method. It is an inbuilt feature for file upload in Selenium. The syntax is as below: WebElement upload_file = driver.


3 Answers

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")
like image 88
falsetru Avatar answered Oct 28 '22 12:10

falsetru


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")
like image 22
vysakh Avatar answered Oct 28 '22 11:10

vysakh


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.

like image 41
user2782827 Avatar answered Oct 28 '22 11:10

user2782827