- Get link
- X
- Other Apps
A Step by Step Guide to Uploading a file using Selenium
We often come across scenarios while testing where we have to upload a file. For different UI there are different ways to upload a file. Following pointers will be covered in this Uploading a FIle using Selenium article:
Moving on with this article on Uploading a file using Selenium
Uploading a file using Selenium
Uploading a file while automating test scripts is no more a challenge. In this tutorial I will be discussing about the most easiest way of uploading a file using Selenium. In this, we will be using “Sendkeys” to upload the file.
Before diving into further details let’s take an example and learn how we can upload a file while automating our scripts using Selenium.
In the above image you can see three things:
Choose File Button: On clicking this button we can choose the file we wish to upload from our machine.
Input type: The Input type of the Choose File button in the above image is of file type.
Upload Button: On clicking, this button upload function is performed.
Note: I would be using an example of edureka where we can upload our image by editing personal details. For this, I need to first login using edureka account. To login, I will be using my username and password, I request you to use yours to practice for the same.
Let’s move forward step by step.

Selenium Certification Training Course
- Instructor-led Sessions
- Assessments
- Assignments
- Lifetime Access
- Set the path of the driver of the browser on which the test script will run.
Example
System.setProperty(“webdriver.chrome.driver”,”/Users/ankita/Downloads/chromedriver”);
- Create an instance of that browser
Example:
WebDriver driver = new ChromeDriver(options);
- Navigate to the Edureka main page and then Login using your username and password
Example:
1 2 3 4 5 | driver.get("https://www.edureka.co/"); driver.findElement(By.linkText("Log In")).click(); driver.findElement(By.id("si_popup_email")).sendKeys("username"); driver.findElement(By.id("si_popup_passwd")).sendKeys("password"); driver.findElement(By.xpath("//*[@id=\"new_sign_up_mode\"]/div/div/div[2]/div[3]/form/button")).click(); |
- After you Login into Edureka account using your username and password. Navigate to page where you can upload your image by editing your personal details.
Here, I am navigating right away to the page from where by pressing image icon I will be navigated to the page to upload image.
On clicking on the highlighted icon above, you will be navigated to the image upload page.
1 2 | driver.get("https://learning.edureka.co/onboarding/personaldetails"); driver.findElement(By.xpath("//*[@id=\"collapseOne\"]/div/div/div[2]/a/i")).click(); |
- As soon as you click on the above mentioned icon, you will be navigated to the page where you can upload the image(as seen below).
Software Testing Training
- Now, without clicking the Choose File button we will use Sendkeys to send the absolute path of the image file which we would like to upload.
Example:
1 2 | WebElement chooseFile = driver.findElement(By.id("custom-input")); chooseFile.sendKeys("/Users/ankita/Downloads/edureka.png"); |
NOTE: If you click on the choose file button, then you will be taken to your machine window to select a file and then you will not be able to select a file using selenium. Hence, you will have to take the help of third tool i.e. either AutoIT or Sikuli.
We will be discussing about them in detail in our other tutorials.
- After you pass the absolute path of the file with sendkeys, the upload button will be enabled and the image selected will be seen as below:
Now, click the upload button:
Example:
driver.findElement(By.cssSelector(“div[class=’submitbtnsec’] > button[type=’submit’]”)).click();
With this uploading of file is done and now the new uploaded image will be seen in your personal details.
Moving on with this article on Uploading a file using Selenium

Selenium Certification Training Course
Weekday / Weekend BatchesBelow is the script to perform the above-mentioned steps in Selenium using Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class EdurekaUploadTest { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver","/Users/ankita/Downloads/chromedriver"); WebDriver driver = new ChromeDriver(options); driver.get("https://www.edureka.co/"); WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.linkText("Log In")))); driver.findElement(By.linkText("Log In")).click(); driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS); wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("si_popup_email")))); driver.findElement(By.id("si_popup_email")).sendKeys("username"); driver.findElement(By.id("si_popup_passwd")).sendKeys("password"); driver.findElement(By.xpath("//*[@id=\"new_sign_up_mode\"]/div/div/div[2]/div[3]/form/button")).click(); Thread.sleep(2000); driver.get("https://learning.edureka.co/onboarding/personaldetails"); Thread.sleep(1000); WebElement imageEdit = driver.findElement(By.xpath("//*[@id=\"collapseOne\"]/div/div/div[2]/a/i")); imageEdit.click(); Thread.sleep(1000); WebElement chooseFile = driver.findElement(By.id("custom-input")); chooseFile.sendKeys("/Users/ankita/Downloads/edureka.png"); Thread.sleep(1000); driver.findElement(By.cssSelector("div[class='submitbtnsec'] > button[type='submit']")).click(); } } |
With this, we come to an end of this Uploading a File Using Selenium article. In this tutorial, we have learned about how to upload a file using “Sendkeys” in selenium. The major drawback with this approach is that we can use sendkeys only when the type of the input is of file type or there is an editable text box along with the browse button. In such a case, we can give the absolute path of the file instead of clicking the button provided to choose a file from the machine.
If you wish to learn Selenium and build a career in the testing domain, then check out our interactive, live-online Selenium Certification here, which comes with 24*7 support to guide you throughout your learning period.
Course Name | Date | |
---|---|---|
Selenium Certification Training Course | Class Starts on 10th September,2022 SAT&SUN (Weekend Batch) | View Details |
Selenium Certification Training Course | Class Starts on 19th September,2022 MON-FRI (Weekday Batch) | View Details |
Selenium Certification Training Course | Class Starts on 8th October,2022 SAT&SUN (Weekend Batch) | View Details |
Recommended videos for you

Quality Assurance With Manual Testing
Watch Now
Selenium Tutorial – A Complete Tutorial on Selenium Automation Testing
Watch Now
Test Automation With Selenium Webdriver
Watch Now
Automated Testing Using Selenium Webdriver
Watch Now
Implementing Blackbox Testing
Watch Now
Selenium WebDriver Tutorial – Get Started With Selenium WebDriver
Watch Now
Selenium Grid Tutorial – Get Started With Selenium Grid
Watch Now
Automate Web Apps Testing With Selenium Webdriver
Watch Now
Top 30 Selenium Interview Questions And Answers
Watch Now
Why Use Selenium with $3 Million Bugs?
Watch Now
Selenium IDE Tutorial – Get Started With Selenium IDE
Watch NowRecommended blogs for you

What is Sanity Testing? Everything That You Need To Know About Sanity Testing
Read Article
Top 50 Manual Testing Interview Questions you Need to know In 2022
Read Article
Comments
Post a Comment