Search code examples
javaselenium-webdriverfile-uploadawtrobot

Robot Class is not working for file upload in IE 11 in Selenium webDriver


I am trying to upload a file in selenium webdriver using java in IE11. Below code is clicking on Browse button but it is not entering or pasting the file name to be uploaded on the newly opened window. It just stucks and nothing happens. Not able to debug the code also.Seems that Robot Class is not responding. I also tried Send keys also but the behaviour is not consistent.

<input name="ctl00$PlaceHolderMain$UploadDocumentSection$ctl05$InputFile" title="Choose a file" class="ms-fileinput ms-fullWidth" id="ctl00_PlaceHolderMain_UploadDocumentSection_ctl05_InputFile" onfocus="ResetSpFormOnSubmitCalled();" onchange="CheckAssetLibMediaExtension()" type="file" size="35">

                 driver.manage().window().maximize(); WebElement element12 = (new WebDriverWait(driver, 10)).until(ExpectedConditions.elementToBeClickable(Main.newdocument(driver))); Main.newdocument(driver).click(); Thread.sleep(500); element12 = driver.findElement(By.xpath("//iframe[@class='ms-dlgFrame']")); driver.switchTo().frame(element12);

          Thread.sleep(2000);


                WebElement element = driver.findElement(By.xpath("//input[@type='file']"));
                JavascriptExecutor executor = (JavascriptExecutor)driver;
                executor.executeScript("arguments[0].click();", element);


                 Robot robot = new Robot();

                 StringSelection sel = new StringSelection("C:\\Users\\m9kuil\\Desktop\\ImportAttendeeTemplate.xlsx");


                   // Copy to clipboard
                 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(sel,null);

                robot.keyPress(KeyEvent.VK_CONTROL);
                 robot.keyPress(KeyEvent.VK_V);

                // Release CTRL+V
                robot.keyRelease(KeyEvent.VK_V);    
                 robot.keyRelease(KeyEvent.VK_CONTROL);

                //Press Enter 
                 robot.keyPress(KeyEvent.VK_ENTER);
                 robot.keyRelease(KeyEvent.VK_ENTER);
                 robot.delay(1000);

Solution

  • Try once by writing robot.delay(10000); before comment //Press Enter

    Other simple alternative for uploading file without robot class :

    driver.findElement(By.xpath(".//input[@type='file']")).sendKeys("C:\\Users\\m9kuil\\Desktop\\ImportAttendeeTemplate.xlsx");
    

    wait(10000)