使用机器人浏览文件 - 有时浏览错误的文件

时间:2017-11-05 09:57:45

标签: java selenium

我需要从我的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);
}

有时它运作良好,有时会显示一个消息:

message trying to open the file 'v'

你知道为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

只需在浏览器中打开此文件,即可:

@Test
public void shouldOpenLocalFileInABrowser() throws InterruptedException {

    WebDriver driver = new ChromeDriver();

    driver.get("file:///C:/tmp/ala.csv");

    Thread.sleep(5000);

    driver.quit();

}

这是上述代码的结果: enter image description here