我需要从PC浏览文件到网络
我使用机器人方法
browseButton.click();
test.log(LogStatus.PASS, "Buttom is clicked");
setClipboardData**("C:\\SLT file\\small.stl**");
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);
public void setClipboardData(String string)
{
StringSelection stringSelection = new StringSelection(string);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
}
有时工作正常,有时显示味精:
你知道为什么会这样吗?
最佳答案
只需在浏览器中以这种方式打开此文件:
@Test
public void shouldOpenLocalFileInABrowser() throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.get("file:///C:/tmp/ala.csv");
Thread.sleep(5000);
driver.quit();
}
这是以上代码的结果: