In this blog, we are going to learn, how we can upload file by using the SendKeys method in Selenium Web Driver.
To upload a file we need to provide an absolute path of
document into the text box where we need to upload the file.
To show the example, I have created one simple HTML page with one input box and the Submit button.
Below is the HTML for the above File upload Page
Below Code Snippet will upload a file using
SendKeys()
Result of above code execution would be
This will work if the file is in local machine, if you are using Grid or Sauce lab kinda the you have use File Detector, Built in method to upload files.
ReplyLocalFileDetector detector = new LocalFileDetector();
((RemoteWebElement)ele).setFileDetector(detector);
ele.sendKeys(detector.getLocalFile("./Resume.pdf")
.getAbsolutePath());
This will work if the file is in local machine, if you are using Grid or Sauce lab kinda the you have use File Detector, Built in method to upload files.
ReplyLocalFileDetector detector = new LocalFileDetector();
((RemoteWebElement)ele).setFileDetector(detector);
ele.sendKeys(detector.getLocalFile("./Resume.pdf")
.getAbsolutePath());