Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in DevOps and Agile by (41.4k points)

How can I upload a file using Selenium WebDriver?

1 Answer

0 votes
by (106k points)

In order to upload a file using Selenium, you can use the below-mentioned code:-

driver.findElement(By.className("browse")).click();

try { 

Thread.sleep(3000);

}catch (InterruptedException e){ 

e.printStackTrace();

}

String fileLocation = System.getProperty("user.dir") + "\\test data\\" + "sample-upload-file.docx";

StringSelection filepath = new StringSelection(fileLocation);

Toolkit.getDefaultToolkit().getSystemClipboard().setContents(filepath, null);

try {

Robot robot = new Robot();

robot.keyPress(KeyEvent.VK_CONTROL);

robot.keyPress(KeyEvent.VK_V);

robot.keyRelease(KeyEvent.VK_V);

robot.keyRelease(KeyEvent.VK_CONTROL);

robot.keyPress(KeyEvent.VK_ENTER);

robot.keyRelease(KeyEvent.VK_ENTER);

} catch (AWTException e) {

e.printStackTrace();

}

You must read the following Selenium Tutorial to learn more about this kind of things. Here is a video tutorial which you must watch through which I came to know about all these things.

.

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.7k users

Browse Categories

...