Selenium | Uploading a file with WebElement and sendKeys

Author - Webner

How to upload files on webpage with selenium webdriver tool?

While uploading a file if it becomes difficult to handle the window dialog box to upload a file on a webpage we can use sendKeys method of WebElement. With sendkeys we have to provide local file path to file upload element.

Like this is a screenshot of a webpage to upload a file:

This is the Selenium code with which we can upload the file instead of actually recording and playing Browse button click:

WebElement element= driver.findElement(By.name("file"));
element.sendKeys("\Users\Easy\Desktop\testfile.txt");
Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *

Send your message