65.9K
CodeProject is changing. Read more.
Home

File Upload using Selenium (Select File on Window)

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (4 votes)

Jan 4, 2019

CPOL
viewsIcon

21985

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