MainMenu

Home Java Overview Maven Tutorials

Sunday, 1 July 2018

How to perform upload operation in selenium

How to upload the file using selenium

Click Here to navigate on practice page



Hello Friends, In this tutorial we will learn how to perform upload operation in selenium.
We can perform the upload operation in selenium by two ways :
1). First by SendKeys() method.
2). Second by using the robot class.
First Method :
Precondition : Element should be input and type should be "file", as shown below :



Now we just need to locate the element and just use the method sendKeys() and in sendKeys() method we will pass the path of that file.
Syntax :-
driver.findElement(By.xpath("path of element")).sendKeys("path on file");

Here is sample code :-

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Uploadfile
{
WebDriver driver;
@BeforeTest
public void g() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "C:\\Users\\cchauhan\\Downloads\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
/*FirefoxOptions foptions = new FirefoxOptions();
foptions.setCapability("marionette", false);
driver = new FirefoxDriver(foptions);*/
driver.manage().window().maximize();
driver.navigate().to("http://www.way2testing.com/p/this-webpage-is-designed-for-selenium.html");
Thread.sleep(3000);
}
@Test
public void h()
{
//Remember element should be input and type should be "file"
driver.findElement(By.xpath(".//*[@id='post-body-2064404811754288590']/form[1]/input[1]")).sendKeys("C:\\Users\\cchauhan\\Desktop\\wallpaper.png");
}
}





Second Method : By using Robot Class:-
What is Robot class ?
"Basically robot class will simulate the Keyboard and mouse actions."
Steps :-
1). First Copy the file path.
2).Paste the file path in popup window
3).Press Enter button.

by robot class we can perform CTRL + C operation by below methods as :-
StringSelection ss = new StringSelection("C:\\Users\\cchauhan\\Desktop\\wallpaper.png");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);

by robot class we can perform CTRL + V operation as below :-
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);



by robot class we can press enter key as below :-
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

Here is sample code :-

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Uploadfile
{
WebDriver driver;
@BeforeTest
public void g() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "C:\\Users\\cchauhan\\Downloads\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
/*FirefoxOptions foptions = new FirefoxOptions();
foptions.setCapability("marionette", false);
driver = new FirefoxDriver(foptions);*/
driver.manage().window().maximize();
driver.navigate().to("http://www.way2testing.com/p/this-webpage-is-designed-for-selenium.html");
Thread.sleep(3000);
}
@Test
public void h() throws AWTException
{
driver.findElement(By.xpath(".//*[@id='post-body-2064404811754288590']/form[1]/input[1]")).click();
Robot robot = new Robot();
robot.setAutoDelay(2000);
StringSelection ss = new StringSelection("C:\\Users\\cchauhan\\Desktop\\wallpaper.png");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
robot.setAutoDelay(2000);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
}



TAGS :-

How to perform control + c operation in selenium

How to upload a file in selenium

Example to upload a file in selenium

upload a file in selenium using robot class

upload a file in selenium using sendkeys methods