-3

I am trying to upload one document from my system to one application and it includes flow as:

Library page >> Upload document button >> Web-based select document pop-up >> after click on select file in pop-up, it open the system pop-up from where need to select file.

for above case, I have written code as:

WebElement element = driver.findElement(By.xpath("//button[@class='btn btn-primary upload-doc-btn cap-letter bold upload_document_image marginrightfix18']")); Actions action = new Actions(driver); action.moveToElement(element).click().perform();

      WebDriverWait wait = new WebDriverWait(driver, 300);
      WebElement selectElement = wait.until(ExpectedConditions
              .visibilityOfElementLocated(By.xpath("//label[@class='btn btn-primary file-select-btn margintop10']")));

      WebElement selectdoc = driver.findElement(By.xpath("//label[@class='btn btn-primary file-select-btn margintop10']"));

      Actions actions = new Actions(driver);
      actions.moveToElement(selectdoc);
      actions.click();

// Up to this click working and getting the system window opened to me, but the given path of file using send keys not working and not able to upload the mentioned file

      actions.sendKeys("C://Users//Vishnu//Documents//seleniumnotes.docx"); 
      actions.build().perform();

For above code, I am getting "unknown error: cannot focus element" error due to the given file not getting selected to upload.

Please suggest your valuable answers. Good if get answer with .sendkeys() method.

I have tried the "Robot" class as well but that as well doe snot worked for me, so trying with .sendkeys().

Your answer can save my day!!

Vishnu More
  • 45
  • 1
  • 12

3 Answers3

1

you can't use selenium to operate window functions. selenium can be used only on browser

so as the robot class is not working i think you should try Autoit software to upload the file

teja
  • 127
  • 2
  • 11
0

Finally, I have successfully uploaded file using Robot class (with help of Actions() )

following code worked for me:

      StringSelection selection = new StringSelection("C:\\Users\\Vishnu\\Documents\\Chapter name.png");
      java.awt.datatransfer.Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
      clipboard.setContents(selection, null);

      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);
Vishnu More
  • 45
  • 1
  • 12
  • I am using C# and using the same code in your question. My problem is that the browse window opens up but does not selects the file and program ends successfully without any errors – Usman Khan Jan 22 '19 at 12:32
0

If your file upload button has input tag then you can use below code to upload file.

WebElement ele = driver.findElement(By.LocStrgy("Locator with input tag"));
ele.sendKeys("path to file");