9

I have a HTML page with button named "Upload" and id: btn-import-questions. The element:

<button class="btn btn-success btn-sm col-lg-11" id="btn-import-questions" data-ts-file-selector="questions-import-init">  Upload&nbsp;<i class="fa fa-upload"></i></button>

I tried a Selenium Java code like this:

driver.findElement(By.id("btn-import-questions")).sendkeys("C:/path/to/file.xlsx");

But since this is an upload button and not an input-type element, the above code is not working.

8
  • What happens when you click this button manually? Commented Jul 17, 2015 at 6:24
  • Window popup will appear immediately and asking for the file path with open and cancel button Commented Jul 17, 2015 at 6:28
  • And when you choose a file using this pop up window, will the name of this file appear somewhere on the page after the pop up disappears? Commented Jul 17, 2015 at 6:30
  • After providing the file path and click on open, suddenly the popup window disappears and the data in the file are imported to my application. Commented Jul 17, 2015 at 6:34
  • Is your window showing uploaded file path..?? Once check it directly on website. Commented Jul 17, 2015 at 6:46

4 Answers 4

8

Check the DOM because somewhere there must be an <input type="file">. The website's javascript will call the .click() of this element to pop up the file selector dialog and closing the dialog with a selection will provide the path. With Selenium the same can be achieved with the .sendkeys():

driver.findElement(By.xpath("//input[@type=\"file\"]")).sendkeys(localFilePath);
Sign up to request clarification or add additional context in comments.

Comments

3

You are doing it almost correctly, except that sendKeys() should be called on the input with type="file" that is, most likely invisible in your case. If this is the case, make the element visible first:

Comments

2

This one works for me:

    String CSVFile = "C:\\D\\Projects\\file.csv";
WebElement fileElement=this.driver.findElement(By.xpath("//[text()='fileElement']"));
            this.wait.until(ExpectedConditions.elementToBeClickable(fileElement ));
            fileElement .click();

            StringSelection ss = new StringSelection(CSVFile);
            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);

            //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);

Comments

-1

You can use AutoIT tool to do this. Use below code in AutoIT .Au3 file to upload.

sleep(1000)
If WinExists("[TITLE:Open]") Then

 Local $hWnd = WinWaitActive ("[TITLE:Open]", "",15)
 WinActivate($hWnd)
 ;WinWaitActive("Open", "", 10)
 ControlFocus("Open","","Edit1")
 ControlsetText("Open","","Edit1",$CmdLine[1])
 ControlClick("Open","","Button1")

ElseIf WinExists("[TITLE:File Upload]") Then

 Local $hWnd = WinWaitActive ("[TITLE:File Upload]", "",15)
 WinActivate($hWnd)
 ;WinWaitActive("Open", "", 10)
 ControlFocus("File Upload","","Edit1")
 ControlsetText("File Upload","","Edit1",$CmdLine[1])
 ControlClick("File Upload","","Button1")

Else

 Local $hWnd = WinWaitActive ("[TITLE:Choose File to Upload]", "",15)
 WinActivate($hWnd)
 ;WinWaitActive("Open", "", 10)
 ControlFocus("Choose File to Upload","","Edit1")
 ControlsetText("Choose File to Upload","","Edit1",$CmdLine[1])
 ControlClick("Choose File to Upload","","Button1")

EndIf

And then use below code in you C# code to invoke it.

String sExe=(<EXE file path>+" "+<Upload file path>);

Runtime.getRuntime().exec(sExe);
Thread.sleep(5000);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.