0

I need to select a file in selenium webdriver from local machine, but as selenium only supports web so I am not able to select file from local machine(Window poup) as it is not having any path in which I can fill the path of file through sendkeys. Is there any solution(using robotics class or autoIT tool or any other solution) from which I can able to select file from window. I have looked into autoIt tool but have not got enough details to implement, if anybody knows about it please reply on this. Please find attached screenshot for reference.

Upload file from local machine window poup after clicking on choose file button

swati
  • 11
  • 1
  • 8
  • Have a look at [AutoIt](https://www.autoitscript.com/site/autoit/) – Guy Aug 16 '17 at 10:59
  • https://www.guru99.com/use-autoit-selenium.html - Autoit. http://www.seleniumeasy.com/selenium-tutorials/webdriver-file-upload-using-robots - selenium Robot class. You can use anyone of it. – Sudha Velan Aug 16 '17 at 11:00
  • Possible duplicate of [How to upload file using Selenium WebDriver in Java](https://stackoverflow.com/questions/16896685/how-to-upload-file-using-selenium-webdriver-in-java) – JeffC Aug 16 '17 at 14:13

3 Answers3

0

Assuming you have filename as "file" in File Upload

WebElement uploadfile = driver.findElement(By.name("file"));
uploadfile.sendKeys(filepath)

driver.findElement(By.id("file")).sendKeys("C:\\path\\to\\file.txt");

where C:\path\to\file.txt is the file location.

0

Just use sendKeys with absolute path of image.

find a tag with type=file , it is not mandatory but selenium support this tag to uplaod file.

Also use waits to prevent from script getting fails

    WebElement button2 = mega.waitsss(driver, upload2);
    button2.sendKeys("C:\\Users\\user\\Desktop\\logo\\Summit-Logo-900px.png");
    button2.click();        
}

WebElement waitsss(WebDriver driver, By elementIdentifier){
     Wait<WebDriver> wait =
                new FluentWait<WebDriver>(driver).withTimeout(60, TimeUnit.SECONDS)
                                                 .pollingEvery(1, TimeUnit.SECONDS)
                                                 .ignoring(NoSuchElementException.class);

        return wait.until(new Function<WebDriver, WebElement>()
                {
                    public WebElement apply(WebDriver driver) {
                           return driver.findElement(elementIdentifier);
                    }
                });
}
Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
  • I can't use sendkeys as it is not having field to give path. Directly clicking on choose file and select file – swati Aug 16 '17 at 11:09
  • you can check after upload a file without click on upload if any element is changing or having value. there is a other option .. refer :- https://stackoverflow.com/questions/40021047/selenium-webdriver-upload-document-to-non-input-button – Shubham Jain Aug 16 '17 at 11:14
  • Else you can use AUTO IT but that make your script windows specific – Shubham Jain Aug 16 '17 at 11:14
0

I have used Robot class and this is much more easy than using AutoIT tool. Please find below code:

import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;

    public void testUpload() throws InterruptedException
        {
            WebElement element = driver.findElement(By.name("uploadfilebutton"));
//can use By cssSelector or name (path) as per convenience
            element.click();
            uploadFile("path to the file");
            Thread.sleep(2000);
        }

        /**
         * This method will set any parameter string to the system's clipboard.
         */
        public static void setClipboardData(String string) {
            //StringSelection is a class that can be used for copy and paste operations.
               StringSelection stringSelection = new StringSelection(string);
               Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
            }

        public static void uploadFile(String fileLocation) {
            try {
                //Setting clipboard with file location
                setClipboardData(fileLocation);
                //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);
            } catch (Exception exp) {
                exp.printStackTrace();
            }
        }
swati
  • 11
  • 1
  • 8