4

This is my first time here but I have already posted this question on SO. I keep getting solutions for Windows + Java and not for Linux or python for that matter.

I am using Selenium and Python to automate a file upload application for the past many hours but nothing helpful!

The HTML DOM structure for this file upload is an input tag with its type attribute value = 'file'. I know I can make use of the send_keys.

The problem is I am not supposed to use send_keys() (as per the question I am trying to solve). The assumption is there are other ways of doing it.

Can anyone suggest me alternative ways to upload the file? I do get solutions for windows but not for Linux(Ubuntu). I know there are applications/tools to automate GUI components like AutoIt but there is nothing for Linux close to AutoIt.

I can say I have done my homework to find a solution. Only after not finding anything useful, I am asking this question, please help!

2

1 Answer 1

3

Welcome to the club, Praveen; your problem is a rite of passage for Selenium users.

Instead of trying to automate the file dialog, you may have better luck issuing the same HTTP request that your file upload form would have issued. Issuing a POST request for a file upload is a little bit harder than a GET request because of the encoding issues, but I suspect you know where to look to find the appropriate Python API.

Another option is to use set_keys() to set the value of the relevant file input element, like this:

elem = driver.find_element_by_id("file")
elem.send_keys("/tmp/temp.txt")
8
  • You are the first ray of hope I have found! Thanks for your reply. was eagerly waiting for it. Let me try your solution.. Will get back to you... Thanks agaain!!! Commented Aug 24, 2013 at 18:31
  • @Praveen See above for a second option.
    – user246
    Commented Aug 24, 2013 at 21:12
  • @ user246: I am trying to solve a question which asks me NOT to use send_keys(). The question is quite sure that there is another option available. I wanted to share my approach that I was trying yesterday which actually is your suggestion. Any file upload to a remote server is nothing but a POST request that we make to that server. In python, we have libraries like urllib and urllib2 which have ways to create a POST request and send it in a Request object. This object will contain the URL where I need to upload the file and the data which is the file itself. Is this approach correct? Commented Aug 25, 2013 at 5:46
  • (Apologies for missing that part of the question.) Yes, that was what I meant. You probably need to use multipart encoding. Of course if there are any other fields in your form, you need to include them too.
    – user246
    Commented Aug 25, 2013 at 12:07
  • Here's what I have written till now: import urllib import urllib2 url = "https://upload.backupgrid.net/add" values = {"file_path":"/home/pinku/Desktop/Test_Upload.odt"} data = urllib.urlencode(values) print data req = urllib2.Request(url, data) response = urllib2.urlopen(req) Commented Aug 25, 2013 at 13:11

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.