Best Practices & Tips: Selenium File Upload

Sauce AI for Test Authoring: Move from intent to execution in minutes.|xBack to ResourcesBlogPosted

April 08, 2026 · 4 min read · Tool Comparison

Sauce AI for Test Authoring: Move from intent to execution in minutes.

|

x

Back to Resources

Blog

Posted March 12, 2019

Best Practices & amp; Tips: Selenium File Upload

Learn how to upload Selenium files utilize Webdriver & # x27; s aboriginal file upload feature.

quote

Learn how to upload a file using Selenium WebDriver with Sauce Labs ’ guide.

In this tutorial, we ’ ll cover how to upload file using Webdriver ’ s native file upload feature.
As you may know, the way to address this in Selenium 1 is to rate your files in an approachable web host and use the attachFile command that points to the right URL. With Selenium 3, the Selenium Dev team has continued the interface from selenium2, using a local file and the sendKeys command.

This works like a appealingness in all drivers. When move this test to a remote server (such as, for example, our Selenium 2 Cloud), all you need to do is use the setFileDetector method to let WebDriver know that you ’ re uploading files from your local computer to a remote waiter instead of simply typecast a path. Almost magically, the file will be base64 encoded and sent transparently through the JSON Wire Protocol for you before writing the fixed outside route. This is an excellent solution, as it lets you switch your trial from a local to remote Driver without having to worry about changing your trial ’ codification.

This feature is useable in all the official Selenium 3 dressing, but make sure Selenium 2.8.0 or raw is used as this lineament has be released then. Here are some model test:

Java

import java.util.;
signification java.net.
;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.Keys;
import org.openqa.selenium.Platform;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
signification org.openqa.selenium.remote.LocalFileDetector;
import java.util.concurrent.TimeUnit;
import java.net.URL;

public unchanging nullity main (String [] args) {

System.setProperty (& quot; webdriver.chrome.driver & quot;, & quot; ./chromedriver & quot;);

Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.

DesiredCapabilities capabillities = DesiredCapabilities.chrome ();
String USERNAME = & quot; USERNAME & quot;;
String ACCESS_KEY = & quot; ACCESS_KEY & quot;;
String url = & quot; https: // & quot; + USERNAME + & quot;: & quot; + ACCESS_KEY + & quot; @ ondemand.saucelabs.com:443/wd/hub & quot;;

URL start_url;
try {
start_url = new URL (url);
} match (MalformedURLException ex) {
return;}

driver.get (& quot; http: //xndev.com/display-image/ & quot;);

RemoteWebDriver driver = new RemoteWebDriver (start_url, capabillities);
driver.setFileDetector (new LocalFileDetector ()); & lt;

WebElement upload = driver.findElement (By.xpath (& quot; // * [@ id= & # x27; post-2554 & # x27;] /div/input & quot;));

//In windows the slash below will need to be reversed
upload.sendKeys (System.getProperty (& quot; user.dir & quot;) + & # x27; / & # x27; + & quot; broken_bulb.jpg & quot;); https: //docs.google.com/document/d/1CnPToQhspWKjHwvF4NVa04-J2KBJaeuPj1dQ3jeK3fk/edit? usp=sharing WebElement img = driver.findElement (By.xpath (& quot; //img [& # x27; #

driver.quit ();

Note to run this codification in windows you will need to change the way of the slash. You ’ ll also need to either add your sauce labs login or else change the comments in the raw code to run it locally.

RUBY

require & # x27; rubygems & # x27; require & quot; test/unit & quot; require & # x27; selenium-webdriver & # x27; classExampleTest & lt; Test: :Unit: :TestCasedefsetup
caps = Selenium: :WebDriver: :Remote: :Capabilities.chrome ()
caps [& # x27; platform & # x27;] = & # x27; Windows 10 & # x27;
cap [& # x27; version & # x27;] = & # x27; 72.0 & # x27;
caps [: name] = & quot; Remote File Upload habituate Selenium 3 with Ruby & quot;
caps [& # x27; selenium-version & # x27;] = & quot; 3.14.0 & quot;
@ driver = Selenium: :WebDriver.for (
:remote,
: url = & gt; & quot; http: //USERNAME: ACCESS_KEY @ ondemand.saucelabs.com:80/wd/hub & quot;,
: desired_capabilities = & gt; caps)
@ driver.file_detector = lambda do|args| # args = & gt; [& quot; /path/to/file & quot;]
str = args.first.to_s
str if File.exist? (str)
endenddeftest_sauce
@ driver.navigate.to & quot; http: //xndev.com/display-image/ & quot;
element = @ driver.find_element (: xpath, & quot; // * [@ id= & # x27; post-2122 & # x27;] /div/input & quot;)
directory = File.expand_path File.dirname (__FILE__)
# Note for windows the slash will need to be switched & quot; # file: //
specific_filename = directory + & quot; / & quot; + & quot; broken_bulb.jpg & quot;
element.send_keys specific_filename
object = @ driver.find_element (: xpath,
& quot; //img [& # x27; #
assert true
end
def teardown
@ driver.quit
end
end

View the origin codehosted with beloved byGitHub

Pro tip: Write your own FileHandler that will use especial codes to represent file before typing paths (locally) or uploading them (remotely). Example: “ test-file: small-image ”

Sauce Labs - Selenium Testing on the Cloud

Sauce Labs makes automated testing awesome. Our cloud-based platform helps developer try aboriginal & amp; intercrossed mobile and web applications across hundreds of browser / OS platforms, include iOS, Android & amp; Mac OS X. Sauce indorse Selenium, Appium and popular JavaScript unit testing frameworks, and integrates with all of the top programming languages, tryout frameworks and CI systems. With built-in video transcription and screenshots of every test lawsuit, debugging tools, and secure tunneling for local or firewalled testing, Sauce makes running, debugging and scale test suites quick and easy.

Published:
Mar 12, 2019
Share this post
Copy Share Link
LinkedIn
© 2026 Sauce Labs Inc., all rightfield 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.
robot
quote

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free