Monday, 7 April 2014

Selenium WebDriver: Handle File Upload

In this post, we'll cover how to upload files using WebDriver.

As we click on 'Browse' button it opens windows ‘File Upload’ dialog, which cannot be handled by WebDriver as it can only handle web based application


So to make it work, first make sure that element is visible and Instead of clicking on Browse button will use sendkeys()

  • Find the xpath of 'Browse' button
  • Enter absolute path of file to upload
  • Find xpath of 'Upload' button and click. 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class FileUpload {
public void fileUpload(WebDriver driver, String filepath) throws Exception {
WebElement fileInput = driver.findElement(By.xpath("xpath_of_browse_button"));
fileInput.sendKeys(filepath);
driver.findElement(By.xpath("xpath_of_upload_button")).click();
}
}
view raw FileUpload.java hosted with ❤ by GitHub

No comments:

Post a Comment