Upload file using Robot class –
Suggested post to read – How to upload file in Selenium WebDriver using sendKeys() method
In this article, we will see how can we upload file in Selenium WebDriver using robot class.
This is another way using which we can upload file. Here we can do key press and key release using robot class.
This method is for handling the Windows File Upload dialog, which cannot be handled using Selenium. Please follow below steps:
1. Click on the File Upload / Choose File button, so that the File Upload dialog is displayed.
driver.findElement(By.xpath(“//*[@id=’uploadfile_0′]”)).click();
2. Copy your file’s absolute path to the clipboard
StringSelection ss = new StringSelection(” C:UsersPrakashDesktopDesktopTOSCA tutorial.pptx”);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
3. Paste the file’s absolute path into the File name field of the File Upload dialog box
//native key strokes for CTRL, V and ENTER keys
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Refer below code –
package basicsOfSelenium; import java.awt.AWTException; import java.awt.Robot; import java.awt.Toolkit; import java.awt.datatransfer.StringSelection; import java.awt.event.KeyEvent; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class FileUploadInSelenium_UsingRobotClass { public static void main(String[] args) throws AWTException { WebDriver driver = new FirefoxDriver(); driver.navigate().to("http://demo.guru99.com/selenium/upload/"); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.manage().window().maximize(); //open upload window driver.findElement(By.xpath("//*[@id='uploadfile_0']")).click(); //put path to your image in a clipboard StringSelection ss = new StringSelection("C://Users//Prakash//Desktop//Desktop//TOSCA tutorial.pptx"); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null); //imitate mouse events like ENTER, CTRL+C, CTRL+V Robot robot = new Robot(); robot.delay(250); robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); robot.keyPress(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_ENTER); robot.delay(90); robot.keyRelease(KeyEvent.VK_ENTER); } }
Refer below articles to learn more about Actions class in Selenium WebDriver –
Actions class in Selenium WebDrivr
MouseHover action in selenium WebDriver
Right Click (Context click) in Selenium
Verify Tool Tip Message text using Selenium WebDriver
How to Perform series of actions using Actions class in selenium WebDriver?
How to use tab button in selenium WebDriver?
How to perform Drag and Drop operation using selenium WebDriver?
How to send text in capital letters into a text-box using Actions class in Selenium WebDriver?
How to refresh webpage using Action class in selenium WebDriver?
Upload file in Selenium Using ROBOT Class
Upload file in Selenium using sendKeys() method
Hope this helps !!