-1

I tried to automate a website using selenium with java. But in the website there is a field for uploading pdf file from the system. How can I upload a file in selenium ?? Here I attach a screenshot of that field Screenshot

Muzzamil
  • 2,823
  • 2
  • 11
  • 23
  • 2
    Does this answer your question? [How to handle windows file upload using Selenium WebDriver?](https://stackoverflow.com/questions/11256732/how-to-handle-windows-file-upload-using-selenium-webdriver) – natn2323 Jan 03 '20 at 07:18
  • Please share HTML of "choose file" button. – Muzzamil Jan 03 '20 at 09:52

2 Answers2

0

Please see the below code , and don't change the focus of the browser by moving the cursor or pressing any keys .

public void navigateToWebSiteAndUploadFile() throws UnsupportedFlavorException, IOException, InterruptedException, AWTException {

     // Create a file object 
    File f = new File("resources\\DemoUpload.txt"); 
    // Get the absolute path of file f 
    String absoluteFilePath = f.getAbsolutePath();  
    //Copy the file path to clipboard
    StringSelection  autoCopiedFilePath = new StringSelection(absoluteFilePath);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(autoCopiedFilePath, null); 

    //Navigate to the URL
    driver.get("https://codepen.io/rcass/pen/MmYwEp");
    driver.switchTo().frame("result"); //switching the frame by name
    //Click on a button which opens the popup 
    driver.findElement(By.xpath("//input[@id='fileToUpload']")).click();
    Thread.sleep(2000);
    //This will paste the file path and name in the file explorer by pressing Ctrl +V combination.
    Robot  robot = new Robot(); 
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);  
    // Pause should be used here to perform the action properly and release the  Ctrl +V keys
    Thread.sleep(2000); 
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);  
    Thread.sleep(8000);
    //press enter key after giving the file path.   
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
    Thread.sleep(5000);
}

Video link-https://www.youtube.com/watch?v=WdtqrVeN1Mk&t=19s

Sameera De Silva
  • 1,722
  • 1
  • 22
  • 41
0

Please try this. This concept enable a input field by Javascript and use it to upload file. You have to provide choose file button locator as webelement and filePath where file exist in your system.

 public void uploadFile(WebElement fileElement, String filePath) {
        JavascriptExecutor executor = (JavascriptExecutor) getDriver();
        String activateField = "arguments[0].style.height='auto'; arguments[0].style.visibility='visible'; arguments[0].style.display='block';";

        executor.executeScript(activateField, fileElement);
        executor.executeScript(activateField, fileElement.findElement(By.tagName("input")));
        fileElement.findElement(By.tagName("input")).sendKeys(filePath);
    }

getDriver() can be replaced by the way you are using driverinstance. I am using it with getDriver() function.

Hope it will help you.

Muzzamil
  • 2,823
  • 2
  • 11
  • 23