Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

File Upload using Selenium (Select File on Window)

0.00/5 (No votes)
4 Jan 2019 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:

IWebDriver driver = new ChromeDriver();

Then 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 seconds 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}");
//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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here