这是我用来通过上传窗口上传图片的代码。在网站上有5个上传按钮,在这里我尝试从两个浏览按钮上传两个图像。但我的问题是,从第一个窗口图像上传和窗口关闭,但从第二个上传窗口图像url被复制但上传窗口没有关闭。
第一个窗口代码:
driver.findElement(By.xpath("//li[@id='upload_1']/input")).click();
StringSelection ss = new StringSelection("D:\\cars\\audi.jpg");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
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);
第二个窗口代码:
driver.findElement(By.xpath("//li[@id='upload_2']/input")).click(); StringSelection ss1 = new StringSelection("C:\\Cars\\audiLights.jpg");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss1, null);
Robot robot1 = new Robot();
robot1.keyPress(KeyEvent.VK_ENTER);
robot1.keyRelease(KeyEvent.VK_ENTER);
robot1.keyPress(KeyEvent.VK_CONTROL);
robot1.keyPress(KeyEvent.VK_V);
robot1.keyRelease(KeyEvent.VK_V);
robot1.keyRelease(KeyEvent.VK_CONTROL);
robot1.keyPress(KeyEvent.VK_ENTER);
robot1.keyRelease(KeyEvent.VK_ENTER);
答案 0 :(得分:4)
是浏览窗口/上传窗口关闭,在最后输入键按下之前添加3-4秒的睡眠。感谢Subh这一切:)