While testing in selenium you may enter a situation where you might have to upload a file for testing. for example, if we consider uploading a file for Olx.in testing there is a file upload required a place where you have to upload a file to test. please find the below snippet for file uploading in selenium.
</p><pre><code>FirefoxDriver driver = new FirefoxDriver();
driver.get("http://olx.in/posting/");
driver.findElement(By.xpath("//*[@id='add-file-1']/div/a/span")).click();
StringSelection stringSelection = new StringSelection("C:\\Users\\tc\\Desktop\\New folder (3)\\Chain1.jpg");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
Thread.sleep(5000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
If you have any doubt comment down below
Arjun is a Full-stack developer, who is fond of the web. Lives in Chikmagalur, Karnataka, India