Click here to Skip to main content
15,892,253 members
Articles / DevOps / Automation
Tip/Trick

File Upload using Selenium (Select File on Window)

Rate me:
Please Sign up or sign in to vote.
3.00/5 (4 votes)
3 Jan 2019CPOL 20.7K   4   2
How to select file for file upload using selenium web driver

Introduction

This tip will help you to select file for file upload using Selenium web driver.

Background

You need basic knowledge of Selenium web driver to use this.

Using the Code

First create web driver object:

C#
IWebDriver driver = new ChromeDriver();

Then find the FileUpload input field by using xpath or id or ny object:

C#
IWebElement ele = driver.FindElement(By.XPath(DOMName));

Click the file upload button:

C#
ele.Click();

Sleep thread for 2 seconds to open the file select window:

C#
Thread.Sleep(2000);

Send the location of the file to be uploaded:

C#
SendKeys.SendWait(FilePath);

Finally, send enter key to select the path:

C#
SendKeys.SendWait("{Enter}");
C++
//Create web driver object
IWebDriver driver = new ChromeDriver();

//Find the FileUpload input field by using xpath or id or ny object
IWebElement ele = driver.FindElement(By.XPath(DOMName)); 

//Click the file upload button
ele.Click();

//Sleep thread for 2 second to open the file select window
Thread.Sleep(2000);

//Send the location of the file to be uploaded
SendKeys.SendWait(FilePath);

//Finally Send enter key to select the path
SendKeys.SendWait("{Enter}");

By using the steps as mentioned above, you will easily select a file for file upload. Also, we have provided a thread sleep for 2 seconds which is not mandatory but you will need it when file selection window comes slow on your machine.

Points of Interest

You will finally select the file to upload it using the Selenium web driver. See check box selection on my next article.

History

  • 4th January, 2019: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
UOM4-Jan-19 23:06
UOM4-Jan-19 23:06 
GeneralRe: My vote of 1 Pin
Jane's Diary7-Jan-19 10:09
Jane's Diary7-Jan-19 10:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.