selenium-webdriverfile-uploadautomationautomated-testsabsolute-path

Selenium Automation Upload file issue


While debugging I found out that my tests fail because of the unloaded file.

I tried many different locators and checked the absolute path to uploading file, but it just doesn't want to upload the file!

It would be great if you will check this form: http://tutorialsninja.com/demo/index.php?route=product/product&path=25_28&product_id=42

Code parts related to issue:

  String path = "C:\\Users\\username\\IdeaProjects\\selenium_bootcamp\\src\\resources\\image.jpg";

  @FindBy(how = How.XPATH, using = "//button[@id='button-upload222']")
public WebElement fileInputApplePage;

 public void uploadFileOnApplePage() {
    fileInputApplePage.sendKeys(path);
}

Solution

  • The type of your input field which you need to upload file is hidden, and you should change it to file, for continuing the upload process

     JavascriptExecutor executor = (JavascriptExecutor)driver;
     executor.executeScript("document.getElementById('input-option222').setAttribute('type', 'file')");
    
     WebElement input = driver.findElement(By.id("input-option222"));
    
     input.sendKeys("C:\\Users\\rober\\IdeaProjects\\selenium_bootcamp\\src\\resources\\image.jpg");
    

    UPDATE: Upload with Robot framework

    driver.findElement(By.id("button-upload222")).click();
    
      
    StringSelection ss = new StringSelection("C:\\Users\\rober\\IdeaProjects\\selenium_bootcamp\\src\\resources\\image.jpg");
    
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
    
            Robot robot = new Robot();
            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.keyRelease(KeyEvent.VK_ENTER);