无法使用Selenium WebDriver上传和裁剪图像

时间:2015-04-01 18:32:21

标签: java selenium selenium-webdriver

我无法使用selenium webdriver上传图片: 我尝试过使用sendkeys,Robot类。 使用Robot类时,它会在打开文件时卡住

这是我的代码:

//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>

2 个答案:

答案 0 :(得分:0)

您拥有的代码应该可以正常工作。你需要使用两个正斜杠。

&#13;
&#13;
     driver.findElement(By.id("image_file")).sendKeys("C://Users//Kanchana//index.png");
&#13;
&#13;
&#13;

答案 1 :(得分:0)

在这里,解决方案尝试了这个并且它工作正常。感谢你们的评论真的有帮助。 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();
      }