Another common elements that we come across in the web sites are file uploads. we can write a java script with the things that we have learnt so far to perform file upload activity. Below simple code will do the file upload.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FileUpload{
public static void main(String[] args) throws Exception {
//Creates a WebDriver object
WebDriver A = new FirefoxDriver();
//Goes to the site
A.get("http://www.freepdfconvert.com/");
Thread.sleep(3000);
//Locate the file browse button and send the file name with "sendKeys() method"
A.findElement(By.xpath("//*[@id='clientUpload']")).sendKeys("D:\\1.pdf");
Thread.sleep(20000);
//Locate the file convert button after upload and hit click()
A.findElement(By.xpath("//*[@id='convertButton']")).click();
}
}
In this example we are using the same "findElement(), sendKeys()" commands that we have used so far in previous examples. Additionally i have used "Thread.sleep(xxxx)" commands. sometimes websites might take more time to load, even to navigate from one page to another page after click, or in this scenario depending on the uploading file size it might need some time to perform. So there for i have given a wait time with "Thread.sleep(xxxx)" commands.
You can simply copy and paste this code in your java compiler and try this out. But make sure to modify the file path in line number 12. good luck
No comments:
Post a Comment