You are on page 1of 8

6/16/2020 A Step by Step Guide to Uploading a file using Selenium | Edureka

A Step by Step Guide to Uploading a le using Selenium


Published on Sep 04,2019 11.8K Views

edureka

We often come across scenarios while testing where we have to upload a le. For di erent UI there are di erent ways to upload
a le. Following pointers will be covered in this Uploading a FIle using Selenium article:

Uploading a le using Selenium


Steps in Selenium using Java

Moving  on with this article on Uploading a le using Selenium

Uploading a le using Selenium


Uploading a le while automating test scripts is no more a challenge. In this tutorial I will be discussing about the most easiest
way of uploading a le using Selenium. In this, we will be using “Sendkeys” to upload the le.

Before diving into further details let’s take an example and learn how we can upload a le 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 le we wish to upload from our machine.

Input type: The Input type of the Choose File button in the above image is of le 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
rst 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.  


FREE WEBINAR

Selenium XPath Tutorial For Beginn…


https://www.edureka.co/blog/uploading-file-usiing-selenium/ 1/8
6/16/2020 A Step by Step Guide to Uploading a file using Selenium | Edureka

Selenium Certi cation Training


Instructor-ledSubscribe
Sessions to our Newsletter, and get personalized recommendations.
×
Assessments
Assignments Sign up with Google
Lifetime Access

Signup with Facebook


Explore Curriculum

Already have an account? Sign in.


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 driver.get("<a href="https://www.edureka.co/">https://www.edureka.co/</a>");
2 driver.findElement(By.linkText("Log In")).click();
3 driver.findElement(By.id("si_popup_email")).sendKeys("username");
4 driver.findElement(By.id("si_popup_passwd")).sendKeys("password");
5 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 driver.get("<a href="https://learning.edureka.co/onboarding/personaldetails">https://learning.edureka
2 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).
 
FREE WEBINAR

Selenium XPath Tutorial For Beginn…


https://www.edureka.co/blog/uploading-file-usiing-selenium/ 2/8
6/16/2020 A Step by Step Guide to Uploading a file using Selenium | Edureka

Subscribe to our Newsletter, and get personalized recommendations.


×
Sign up with Google

Signup with Facebook

Already have an account? Sign in.

ftware Testing Training

SELENIUM MOBILE APP PERFORMANCE MANUAL TE

󡁳 CERTIFICATION
TRAINING 󡞗 TESTING USING
APPIUM 󡠑 TESTING USING
JMETER 󡄆 CERTIFICATI
TRAINING

Selenium Certi cation Mobile App Testing Using Performance Testing Manual Testing
Training Appium Using JMeter Certi cation Trainin

Reviews Reviews Reviews Reviews

     5(25047)      5(1857)      5(1786)      5(4389)

Now, without clicking the Choose File button we will use Sendkeys to send the absolute path of the image le which we
would like to upload.

Example:

1 WebElement chooseFile = driver.findElement(By.id("custom-input"));


2 chooseFile.sendKeys("/Users/ankita/Downloads/edureka.png");

NOTE: If you click on the choose le button, then you will be taken to your machine window to select a le and then you will not
be able to select a le 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 le with sendkeys, the upload button will be enabled and the image selected will be
seen as below:

 
FREE WEBINAR

Selenium XPath Tutorial For Beginn…


https://www.edureka.co/blog/uploading-file-usiing-selenium/ 3/8
6/16/2020 A Step by Step Guide to Uploading a file using Selenium | Edureka

Subscribe to our Newsletter, and get personalized recommendations.


×
Sign up with Google

Signup with Facebook

Already have an account? Sign in.

Now, click the upload button:

Example:
driver. ndElement(By.cssSelector(“div[class=’submitbtnsec’] > button[type=’submit’]”)).click();

With this uploading of le is done and now the new uploaded image will be seen in your personal details.

Moving  on with this article on Uploading a le using Selenium

Selenium Certi cation Training


Weekday / Weekend Batches

See Batch Details

Below is the script to perform the above-mentioned steps in Selenium using Java

 
FREE WEBINAR

Selenium XPath Tutorial For Beginn…


https://www.edureka.co/blog/uploading-file-usiing-selenium/ 4/8
6/16/2020 A Step by Step Guide to Uploading a file using Selenium | Edureka

1 import java.util.concurrent.TimeUnit;
2 import org.openqa.selenium.By;
3 import org.openqa.selenium.WebDriver;
4 import org.openqa.selenium.WebElement;
5
6
import
import
org.openqa.selenium.chrome.ChromeDriver;
Subscribe to our Newsletter, and get personalized recommendations.
org.openqa.selenium.chrome.ChromeOptions;
×
7 import org.openqa.selenium.support.ui.ExpectedConditions;
8 import org.openqa.selenium.support.ui.WebDriverWait;
9 Sign up with Google
10 public class EdurekaUploadTest {
11
12 public static void main(String[] args) throws InterruptedException {
Signup with Facebook
13
14 System.setProperty("webdriver.chrome.driver","/Users/ankita/Downloads/chromedriver");
15 WebDriver driver = new ChromeDriver(options);
16 Already have an account? Sign in.
driver.get("<a href="https://www.edureka.co/">https://www.edureka.co/</a>");
17
18 WebDriverWait wait = new WebDriverWait(driver, 10);
19 wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.linkText("Log In"))));
20 driver.findElement(By.linkText("Log In")).click();
21 driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
22 wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("si_popup_email"))));
23 driver.findElement(By.id("si_popup_email")).sendKeys("username");
24 driver.findElement(By.id("si_popup_passwd")).sendKeys("password");
25
26 driver.findElement(By.xpath("//*[@id=\"new_sign_up_mode\"]/div/div/div[2]/div[3]/form/button")).click
27
28 Thread.sleep(2000);
29 driver.get("<a href="https://learning.edureka.co/onboarding/personaldetails">https://learning.edureka
30 Thread.sleep(1000);
31
32 WebElement imageEdit = driver.findElement(By.xpath("//*[@id=\"collapseOne\"]/div/div/div[2]/a/i"));
33
34 imageEdit.click();
35 Thread.sleep(1000);
36 WebElement chooseFile = driver.findElement(By.id("custom-input"));
37 chooseFile.sendKeys("/Users/ankita/Downloads/edureka.png");
38 Thread.sleep(1000);
39 driver.findElement(By.cssSelector("div[class='submitbtnsec'] > button[type='submit']")).click();
40
41 }
42
43 }

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 le 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 le 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 le instead of clicking the button provided to choose a le 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 3.0
Certi cation Training here, that comes with 24*7 support to guide you throughout your learning period.

Recommended videos for you

   
Selenium WebDriver Tutorial – Why Use Selenium with $3 Automate Web Apps Testing Selenium Tutorial –
Get Started With Selenium Million Bugs? With Selenium Webdriver Complete Tutorial o
WebDriver Selenium Automatio

Watch Now Watch Now Watch Now Watch Now

‹›

Recommended blogs for you

 
FREE WEBINAR

Selenium XPath Tutorial For Beginn…


https://www.edureka.co/blog/uploading-file-usiing-selenium/ 5/8
6/16/2020 A Step by Step Guide to Uploading a file using Selenium | Edureka

Arti cial Intelligence BI and Visualization Big Data Blockchain Cloud Computing Cyber Security Data Science

Data Warehousing and ETL Databases DevOps Digital Marketing Front End Web Development Mobile Development

Operating Systems Programming & Frameworks Subscribe


Project Management andto our Newsletter,
Methodologies andProcess
Robotic get personalized
Automation recommendations.
×
Systems & Architecture Sign up with Google

Signup with Facebook


Already have an account? Sign in.
TRENDING CERTIFICATION COURSES TRENDING MASTERS COURSES

DevOps Certi cation Training Data Scientist Masters Program

AWS Architect Certi cation Training DevOps Engineer Masters Program

Big Data Hadoop Certi cation Training Cloud Architect Masters Program

Tableau Training & Certi cation Big Data Architect Masters Program

Python Certi cation Training for Data Science Machine Learning Engineer Masters Program

Selenium Certi cation Training Full Stack Web Developer Masters Program

PMP® Certi cation Exam Training Business Intelligence Masters Program

Robotic Process Automation Training using UiPath Data Analyst Masters Program

Apache Spark and Scala Certi cation Training Test Automation Engineer Masters Program

Microsoft Power BI Training Post-Graduate Program in Arti cial Intelligence & Machine Learning

Online Java Course and Training Post-Graduate Program in Big Data Engineering

Python Certi cation Course

COMPANY WORK WITH US

About us Careers

News & Media Become an Instructor

Reviews Become an A liate

Contact us Become a Partner

Blog Hire from Edureka

Community
DOWNLOAD APP
Sitemap

Blog Sitemap

Community Sitemap

Webinars

CATEGORIES 

CATEGORIES
Cloud Computing DevOps Big Data Data Science BI and Visualization Programming & Frameworks Software Testing

Project Management and Methodologies Robotic Process Automation Frontend Development Data Warehousing and ETL Arti cial Intelligence Blockchain

Databases Cyber Security Mobile Development Operating Systems Architecture & Design Patterns Digital Marketing

TRENDING BLOG ARTICLES 

TRENDING BLOG ARTICLES


Selenium tutorial Selenium interview questions Java tutorial What is HTML Java interview questions PHP tutorial JavaScript interview questions

Spring tutorial PHP interview questions Inheritance in Java Polymorphism in Java Spring interview questions Pointers in C Linux commands

Android tutorial JavaScript tutorial jQuery tutorial SQL interview questions MySQL tutorial Machine learning tutorial Python tutorial

What is machine learning Ethical hacking tutorial SQL injection AWS certi cation career opportunities AWS tutorial What Is cloud computing

What is blockchain Hadoop tutorial What is arti cial intelligence Node Tutorial Collections in Java Exception handling in java

Python Programming Language Python interview questions Multithreading in Java ReactJS Tutorial Data Science vs Big Data vs Data Analy…

Software Testing Interview Questions R Tutorial Java Programs JavaScript Reserved Words and Keywords Implement thread.yield() in Java: Examp…

Implement Optical Character Recogniti… All you Need to Know About Implement…
 
FREE WEBINAR
  Selenium
 XPath Tutorial For Beginn…
https://www.edureka.co/blog/uploading-file-usiing-selenium/ 7/8
6/16/2020 A Step by Step Guide to Uploading a file using Selenium | Edureka

© 2020 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved. Terms & Conditions

Legal & Privacy

Subscribe to our Newsletter, and get personalized recommendations.


×
Sign up with Google
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of
MongoDB, Inc.

Signup with Facebook

Already have an account? Sign in.

 
FREE WEBINAR

Selenium XPath Tutorial For Beginn…


https://www.edureka.co/blog/uploading-file-usiing-selenium/ 8/8
6/16/2020 A Step by Step Guide to Uploading a file using Selenium | Edureka

Subscribe to our Newsletter, and get personalized recommendations.


×
Sign up with Google
Top Selenium Interview Everything You Need to Know All you need to know about A User’s Perspective
Questions For Beginners In About Waits in Selenium WebElement in Selenium Selenium IDE
2020
Signup with Facebook

Read Article Read Article Read Article Read Article


Already have an account? Sign in.

‹›

Comments 0 Comments

ALSO ON HTTPS://WWW.EDUREKA.CO/BLOG/

How to become a Social Media Create Angular App Confi


Digital Marketer? … Marketing: An … with Bootstrap Free T

7 months ago • 1 comment 5 months ago • 3 comments 4 months ago • 1 comment 7 mont
This article on How to In the world of smartphones This article talks about how The Ed
become a Digital Marketer and Artificial Intelligence, it you can incorporate is on B
will help you understand … is quite important to … Bootstrap functionality … is a ste

1 Comment https://www.edureka.co/blog/ 🔒 Disqus' Privacy Policy 


1 Login

 Recommend t Tweet f Share Sort by Best

Join the discussion…

LOG IN WITH
OR SIGN UP WITH DISQUS ?

Name

Anonymous • a month ago


How we can upload a file, if the sendkeys is not working with the element. i.e elements are
working only using send javascriptexecutor or Action class.
△ ▽ • Reply • Share ›

✉ Subscribe d Add Disqus to your siteAdd DisqusAdd ⚠ Do Not Sell My Data

Trending Courses in Software Testing

SELENIUM
CERTIFICATION TRAINING

Selenium Certi cation Mobile App Testing Using Performance Testing Using Manual Testing C
Training Appium JMeter Training
 26k Enrolled Learners  2k Enrolled Learners  2k Enrolled Learners  5k Enrolled Learners
 Weekend/Weekday  Weekend  Weekend  Weekend
 Live Class  Live Class  Live Class  Live Class

Reviews Reviews Reviews Reviews

 5 (10050)  5 (750)  5 (750)  5 (1800)


 
FREE WEBINAR
‹›
Browse Categories Selenium XPath Tutorial For Beginn…
https://www.edureka.co/blog/uploading-file-usiing-selenium/ 6/8

You might also like