Back to Resources


Posted November 14, 2009

Selenium Tips: Upload files on browsers running over remote machines


Having your tests upload files to the test application is something that becomes confusing when moving Selenium RC servers to different machines. Browsers can only upload files from the local machine, but when the RC server (and consequently the browser) are on a different machine than the one running the test, this can be very hard to do. Luckily, for this kind of situations, Selenium has the attach_file method which receives both a locator and a URL as parameters, then the RC Server takes care of downloading the file to the test machine at a temporary location and then attaching the file completing with this path in the desired field. Here's the reference from Selenium's documentation:

In [5]: sel.attach_file? Definition:    a.attach_file(self, fieldLocator, fileLocator) Docstring: Sets a file input (upload) field to the file listed in fileLocator 'fieldLocator' is an element locator 'fileLocator' is a URL pointing to the specified file. Before the file  can be set in the input field (fieldLocator), Selenium RC may need to transfer the file to the local machine before attaching the file in a web page form. This is common in selenium  grid configurations where the RC server driving the browser is not the same  machine that started the test. Supported Browsers: Firefox ("*chrome") only.

Keep in mind that for using this method the files to upload will have to be placed in a public URL. However, if you don't want them to be 100% public, you can still keep them safe using basic HTTP Auth and get them using the auth information in the same URL. Here's an example usage of the command in python:

sel.attach_file("css=input[type=file]", "")

Some caveats in Selenium's implementation, is that this only works on firefox and that also the file must be placed on top level in the URL, so using the following URL will not work:

while the following will:

In Sauce OnDemand, we took care of fixing both issues + adding ftp support too, so if you used Sauce OnDemand, just put your files in a public server (both HTTP or FTP) and then just use attach_file to upload them. Hope you find this useful, and if you need more info, let us know in the comments.

Nov 14, 2009
Share this post
Copy Share Link
© 2023 Sauce Labs Inc., all rights reserved. SAUCE and SAUCE LABS are registered trademarks owned by Sauce Labs Inc. in the United States, EU, and may be registered in other jurisdictions.