Fail Upload file in Selenium webdriver using Robot class

1k Views Asked by At

i am pressing at open button , that brings the windows dialog , and i want to select from it an image to upload . (driver.findElement(By.id("myID")).sendKeys("C:\\myPhoto.jpg"); NOT WORKING )

i tried using robot class , but it does nothing .

     //Image upload code
      driver.findElement(By.id("image_file"));
      driver.findElement(By.id("image_file")).click();  \\ window dialog opens
      uploadImage("C:\\myPhoto.jpg");
      Thread.sleep(500);

  public static void setClipBoardData(String string){
      //Copying the path of the file to the clipboard 
      //StringSelection class used for copy and paste operations.
      StringSelection stringselect = new StringSelection(string);//Putting the path of the image to upload
      Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringselect, null);
  }

  public static void uploadImage(String imagelocation){
      try{
          //Setting clipboard with image location
          setClipBoardData(imagelocation);
        //Some sleep time to detect the window popup
          Thread.sleep(500);
          //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);
        //To Click on the "Open" button to upload files
          robot.keyPress(KeyEvent.VK_ENTER);
          robot.keyRelease(KeyEvent.VK_ENTER);
          robot.delay(500);
      } catch (Exception exp) {
          exp.printStackTrace();
      }

HTML - here

     HTML  -<div id="thumbnail" class="toolTipTranslate left-Arrow coverImageDimension" top="-=535px" left="580px" rtl-left="-702px" translatekey="ACTIVITY_DETAILS_IMAGE_TIP" width="264px">
<input id="activityIconFocusField" type="text" onclick="$('#thumbnailUploadImage').trigger('click');" readonly="" name="activityIcon">here
0

There are 0 best solutions below