0

I am not able to upload an image using selenium webdriver: I have tried it doing with sendkeys, Robot class. With Robot class it just get stuck while opening file

here is my code:

//Image upload code
      driver.findElement(By.id("image_file"));
      driver.findElement(By.id("image_file")).click();
     //driver.findElement(By.id("image_file")).sendKeys("C:/Users/Kanchana/index.png");
      uploadImage("index.png");
      Thread.sleep(2000);
      driver.findElement(By.id("companylogo_save_btn")).click();
      driver.findElement(By.cssSelector("span.doneedit > button")).click();
      driver.findElement(By.xpath("//html/body/div[4]/div/div[1]/div[1]/div/img")).click();
      driver.findElement(By.cssSelector("span.comment")).click();
  }

  public static void setClipBoardData(String string){
      //StringSelection class used for copy and paste operations.
      StringSelection stringselect = new StringSelection(string);
      Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringselect, null);
  }

  public static void uploadImage(String imagelocation){
      try{
          //Setting clipboard with image location
          setClipBoardData(imagelocation);

          //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);
      } catch (Exception exp) {
          exp.printStackTrace();
      }

HTML:

<span class="fileinputs"> <input id="image_file" type="file" name="fileUpload"> <label>
angel
  • 11
  • 4

2 Answers2

0

The code you have should work fine. You need to use two forward slashes.

     driver.findElement(By.id("image_file")).sendKeys("C://Users//Kanchana//index.png");
CoffeeTime
  • 305
  • 1
  • 6
  • 17
  • I already tried image uploading through sendkeys- not working for me. Then I tried with robot class and this also isn't working I have tried with two slashes too. It goes to image file opening page copy's the path of the image file in the open file text box and doesn't go further and in JUnit the test case is passed without uploading an image. – angel Apr 03 '15 at 17:34
0

Here, is the solution tried out this and it works fine. Thank guys for your comments really helped. Thanx

    //Image upload code
      driver.findElement(By.id("image_file"));
      driver.findElement(By.id("image_file")).click();
      uploadImage("C:\\Users\\Kanchana\\person.jpg");
      Thread.sleep(500);
      driver.findElement(By.id("companylogo_save_btn")).click();
      driver.findElement(By.cssSelector("span.doneedit > button")).click();
      driver.findElement(By.xpath("//html/body/div[4]/div/div[1]/div[1]/div/img")).click();
      driver.findElement(By.cssSelector("span.comment")).click();
  }

  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();
      }
angel
  • 11
  • 4