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>