2015-04-01 74 views
0

我无法使用selenium webdriver上传图像: 我尝试过使用sendkeys,Robot类来做这件事。 随着机器人类,同时打开文件无法使用Selenium WebDriver上传和裁剪图像

这只是卡住是我的代码:

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

请发表相关的html请! – LittlePanda

+0

使用机器人类也是你做错了。提供文件的完整路径或相对路径,而不仅仅是文件名。 –

+0

@LittlePanda这里是HTML代码

回答

0

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

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

+0

我已经尝试过通过sendkeys上传图片 - 不适合我。然后,我尝试了机器人课,这也不起作用,我也尝试了两个斜杠。它转到图像文件打开页面副本的图像文件在打开的文件文本框中的路径,并没有更进一步,并在JUnit测试用例传递没有上传图像。 – angel

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(); 
     }