[英]Unable to upload and crop image using Selenium WebDriver
I am not able to upload an image using selenium webdriver: I have tried it doing with sendkeys, Robot class. 我无法使用Selenium WebDriver上载图像:我已经尝试过使用sendkeys,Robot类进行此操作。 With Robot class it just get stuck while opening file
使用Robot类,它在打开文件时会卡住
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: HTML:
<span class="fileinputs"> <input id="image_file" type="file" name="fileUpload"> <label>
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");
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.