Java 如何在不使用robot的情况下关闭打开的文件

Java 如何在不使用robot的情况下关闭打开的文件,java,selenium,selenium-webdriver,selenium-chromedriver,Java,Selenium,Selenium Webdriver,Selenium Chromedriver,我正在使用Selenium测试一个网站。我可以上传一个“.txt”文件,然后双击它打开,但是我不能用selenium关闭打开的文件 我知道使用Alt+F4可以解决robot工具的问题,但我不允许使用robot,我尝试了下面的selenium代码来关闭窗口,它不起作用: action.sendKeys(Keys.chord(Keys.ALT,Keys.F4)).build().perform(); 试试这个(driver是WebDriver的一个实例): 这将存储原始窗口的句柄并切换回它。如果调

我正在使用Selenium测试一个网站。我可以上传一个“.txt”文件,然后双击它打开,但是我不能用selenium关闭打开的文件

我知道使用Alt+F4可以解决robot工具的问题,但我不允许使用robot,我尝试了下面的selenium代码来关闭窗口,它不起作用:

action.sendKeys(Keys.chord(Keys.ALT,Keys.F4)).build().perform();
试试这个(
driver
WebDriver
的一个实例):

这将存储原始窗口的句柄并切换回它。如果调用
driver.quit(),则另一个窗口可能仍在后台打开,但将关闭

感谢:


您是否尝试过使用其他工具,如autoit或sikuli@skumarsikuli的问题是,我第一次运行该程序时,它抛出异常并创建一个指向其dll文件的“路径”,我不希望它发生在客户的计算机上:(任何我们可以添加“路径”和“libs”的方式)在可执行的jar文件中,我不会抛出异常,也不会更改计算机上的任何内容?您的答案是,我没有关闭窗口,它会在新打开的窗口的左上角打开一个小窗口,并说“禁用开发人员模式扩展…”好的,这与第一个答案基本相同。但是请注意,
getWindowHandles()
返回所有当前窗口。您的代码依赖于一个假设,即最后一个窗口始终是新打开的窗口。可能是这样,也可能不是这样。
String myWindow = driver.getWindowHandle();

//open file with double click
//do something ...

driver.switchTo().window(myWindow);
String parentHandle = driver.getWindowHandle(); // get the current window handle
driver.findElement(By.xpath("//*[@id='someXpath']")).click(); // click some link that opens a new window

for (String winHandle : driver.getWindowHandles()) {
    driver.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window)
}

//code to do something on new window

driver.close(); // close newly opened window when done with it
driver.switchTo().window(parentHandle); // switch back to the original window


  [1]: https://stackoverflow.com/questions/19112209/how-to-handle-the-new-window-in-selenium-webdriver