Upload files is a very general function on the web. But when you want to implement it in the selenium testing automation script, a dialog box will always pop up which selenium can not handle. This article will tell you 3 methods to use selenium webdriver to upload files on an Html web page.
1. Selenium Upload File In Source Code Directly Steps.
- Locate the Html upload file web element in selenium webdriver source code.
WebElement element = driver.findElement(By.name("uploadFileInputBox"));
- Send the uploaded file’s absolute path ( as string ) to the upload file web element.
element.sendKeys("C:/Workspace/uploadFileTest.txt");
- Click the Html form submit button to submit the Html form.
WebElement submitBtn = driver.findElement(By.name("uploadFileSubmitBtn")); submitBtn.click();
- Create an eclipse Maven project, create a new java class com.dev2qa.webdriver.TestUploadFile and an Html file uploadFileTest.html. Below is the maven project file structure.
> tree /F ./ │ pom.xml │ ├─src │ ├─main │ │ ├─java │ │ │ └─com │ │ │ └─dev2qa │ │ │ └─webdriver │ │ │ │ TestUploadFile.java │ │ │ │ uploadFileTest.html
- Below is the TestUploadFile.java file source code.
package com.dev2qa.webdriver; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class TestUploadFile { public static void testUploadFile() { try { // Set the geckodriver.exe path to the system property webdriver.gecko.driver's value. System.setProperty("webdriver.gecko.driver", "D:\\Work\\Tool\\geckodriver-v0.29.1-win64\\geckodriver.exe"); /* This is the WebDriver object used to manipulate web browsers.*/ WebDriver driver; /* This page displays upload file web element, * this is just a fake one. You can specify your real page.*/ String uploadFileDisplayUrl = "http://www.dev2qa.com/demo/upload/uploadFileTest.html"; // Initialize the WebDriver object use FirefoxDriver. driver = new FirefoxDriver(); // Maximize the Firefox browser window. driver.manage().window().maximize(); // Use Firefox web browser to load and display the upload file web page. driver.get(uploadFileDisplayUrl); // Get the upload file web element by it's name "uploadFileInputBox" WebElement element = driver.findElement(By.name("uploadFileInputBox")); //Input the uploaded file's absolute file path to the upload file web element as string use sendKeys() method. element.sendKeys("D:\\Work\\test.jpg"); Thread.sleep(3000); // Find the Html form submit button in the web page. WebElement submitBtn = driver.findElement(By.name("uploadFileSubmitBtn")); // Click the submit button to upload the file. submitBtn.click(); // Get the return message in the returned web page. String checkText = driver.findElement(By.id("returnMessage")).getText(); if("Your file has been uploaded successful".equalsIgnoreCase(checkText)) { System.out.println("Upload file success."); } }catch(InterruptedException ex) { ex.printStackTrace(); } } public static void main(String[] args) { TestUploadFile.testUploadFile(); } }
- Below is the Html file uploadFileTest.html content.
<!DOCTYPE html> <html> <head> <title>This is a upload file demo page.</title> </head> <body> <!--This is just the html form which include the upload file textbox --> <form enctype="multipart/form-data" action="http://www.dev2qa.com/demo/upload/receiveFile.html" method="post"> <p>Browse and upload file: </p> <input type="file" name="uploadFileInputBox"> <br/><br/> <!-- This is the form submit button --> <input type="submit" value="Upload File" name="uploadFileSubmitBtn"> </form> </body> </html>
- This method works only when the Html upload file input textbox is visible and enabled.
- You should not click the Choose File button for the upload file web element. If you click it, a windows dialog will pop up, this can not be handled by selenium.