Uploading file through Selenium Webdriver can be done through following steps:
Step 1:- Set the path of chromeDriver exe file:
System.setProperty("webdriver.chrome.driver",”./driver/chromedriver.exe");
Step 2:- Create an object of ChromeDriver Class :
WebDriver driver = new ChromeDriver();
Here we are doing upcasting, converting subclass object into Super Type.
Step 3:- Navigate to the web page where you have to upload the file:-
driver. navigate.to ("pass the URL of the page");
Now you have to find the upload button on the web page, use any locator(XPath, id or class name, etc..) to find that button and click on it.
WebElement uploadButton = driver.findElement(By.id("uploadBTN"));
uploadButton.click();
Step 4:- Now Enter the path of the file which you have to upload on WebPage:
WebElement uploadTextField = driver.findElement(By.id("uploadTextField"));
uploadTextField.sendKeys("D:\Screenshot_1.png”);
we are passing the path of .png file to upload on the web page.
Wait till the file is uploading on the page after successfully load the file click on submit button.
driver.findElement(By.name("submitBTN")).submit();
Once you run this script, you will be able to upload the file effectively and get a confirmation message (Your file is uploaded and unpacked).
I hope the above steps would have given you a perspective to handle file upload using Selenium WebDriver, in your Selenium projects. If you have a unique idea or perspective to work with the file uploads, then please let us know.