Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
share|improve this question
    
you can try javascript? like `((JavascriptExecutor) driver).executeScript("document.getElementById('activityIconFocusField').text='p‌​ath';"); –  Kavan yesterday
    
I tried your uploadimage function after opening google page, it works fine. so It seems that issue is with the focus of textbox, which you can try to get by pressing tab or some shortcut key(like alt+n or alt+f if available). –  Kavan yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.