0
<input type="button" class="button white-button custom-modal-button" id="btnAttachment" ng-click="openAttachment()" value="Import CSV template">

enter code here

WebElement browse =driver.findElement(By.xpath("//*[@id=\"btnAttachment\"]"));
//pass the path of the file to be uploaded using Sendkeys method
browse.sendKeys("\\Users\\nilaapps13\\Desktop\\lead.csv");

when using sendkeys function it opens up the upload window , but not choosing the file . is there any other way?

1
  • If you close the opened window, then it is choosing or not? Commented Jan 24, 2019 at 15:07

3 Answers 3

1
File file = new File("/users/chennai4/downloads/lead.csv");
            StringSelection stringSelection= new StringSelection(file.getAbsolutePath());
           //Copy to clipboard 
            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
            Robot robot = new Robot();
          // Cmd + Tab is needed since it launches a Java app and the browser looses focus
            robot.keyPress(KeyEvent.VK_META);
            robot.keyPress(KeyEvent.VK_TAB);
            robot.keyRelease(KeyEvent.VK_META);
            robot.keyRelease(KeyEvent.VK_TAB);
            robot.delay(500);

            //Open Goto window
            robot.keyPress(KeyEvent.VK_META);
            robot.keyPress(KeyEvent.VK_SHIFT);
            robot.keyPress(KeyEvent.VK_G);
            robot.keyRelease(KeyEvent.VK_META);
            robot.keyRelease(KeyEvent.VK_SHIFT);
            robot.keyRelease(KeyEvent.VK_G);

            //Paste the clipboard value
            robot.keyPress(KeyEvent.VK_META);
            robot.keyPress(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_META);
            robot.keyRelease(KeyEvent.VK_V);

            //Press Enter key to close the Goto window and Upload window
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);
            robot.delay(1000);
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);

this is the code i used working perfectly in mac os..

Sign up to request clarification or add additional context in comments.

Comments

0

I think you have to press enter ie send Enter key using sendKeys

Comments

0

the upload window is a Windows popup and not browser popup so selenium commands dont work in this case.

You can make use of java Robot and StringSelection class. Step 1: copy file path to system clipboard Step 2: paste file path to upload window (send keys Ctrl+V) and then send Enter key.

add following packages

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

use StringSelection class for copy and paste operations.

StringSelection stringSelection = new StringSelection("\\Users\\nilaapps13\\Desktop\\lead.csv");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);

use Robot class to send keyboard events

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

1 Comment

This is the best way . it worked perfectly fine ... ty :-)

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.