【发布时间】:2018-05-14 15:07:50
【问题描述】:
我要上传一个文件,上传按钮的代码如下:
<button class="panel-box-header__controls-bttn bttn-icon js-doc-upload" type="button">
<svg class="icon icon-clip">
<use xlink:href="#icon-clip"></use>
</svg>
</button>
方法driver.findElement(By.xpath("...")).sendKeys("path of the file which u want to upload"); 在这里不起作用
Method Robot 也不起作用:
public void upload() throws Exception {
uploadButton.click();
Thread.sleep(2000);
//File Need to be imported
File file = new File("/Users/admin/Desktop/test_image.jpg");
StringSelection stringSelection= new StringSelection(file.getAbsolutePath());
//Copy to clipboard
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
Robot robot = new Robot();
// Cmd + Tab is needed since it launches a Java app and the browser looses focus
robot.keyPress(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_META);
robot.keyRelease(KeyEvent.VK_TAB);
robot.delay(500);
//Open Goto window
robot.keyPress(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_G);
robot.keyRelease(KeyEvent.VK_META);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.keyRelease(KeyEvent.VK_G);
//Paste the clipboard value
robot.keyPress(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_META);
robot.keyRelease(KeyEvent.VK_V);
//Press Enter key to close the Goto window and Upload window
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.delay(500);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
当我使用上面示例中的 Robot 方法时,单击上传按钮,然后打开一个带有文件的窗口,并且没有任何反应,指定的文件没有上传。
也许有人可以解决这个问题?使用 Robot 方法或任何其他选项
【问题讨论】:
标签: java selenium-webdriver file-upload junit automated-tests