【问题标题】:How to close an opened file without using robot如何在不使用机器人的情况下关闭打开的文件
【发布时间】:2015-06-05 17:52:23
【问题描述】:

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

我知道有一个使用 Alt+F4 的机器人工具的解决方案,但我不允许使用机器人,我尝试了下面的 selenium 代码关闭窗口,它不起作用:

action.sendKeys(Keys.chord(Keys.ALT,Keys.F4)).build().perform();

【问题讨论】:

  • 您是否尝试过使用其他工具,例如 autoit 或 sikuli
  • @skumar sikuli 的问题是我第一次运行程序时它会抛出异常并为其 dll 文件创建一个“PATH”,我不希望它发生在客户的计算机上:(我们可以在可执行 jar 文件中添加“PATH”和“libs”的任何方式,我不会抛出异常并且不会更改计算机上的任何内容?您的回答必须赞赏

标签: java selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

试试这个(driverWebDriver 的一个实例):

String myWindow = driver.getWindowHandle();

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

driver.switchTo().window(myWindow);

这会存储原始窗口的句柄并切换回它。如果您致电driver.quit();,其他窗口可能仍在后台打开但将关闭

【讨论】:

  • 我没有关闭窗口,它在新打开的窗口左上角打开一个小窗口,并显示“禁用开发者模式扩展...”
【解决方案2】:

感谢:CODEBLACK

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

【讨论】:

  • 好的,这与第一个答案基本相同。但请注意,getWindowHandles() 返回所有当前窗口。您的代码依赖于假设,即最后一个始终是新打开的窗口。可能是这样 - 也可能不是。
猜你喜欢
  • 1970-01-01
  • 2014-11-06
  • 2020-11-09
  • 2010-11-04
  • 2017-03-04
  • 1970-01-01
  • 1970-01-01
  • 2013-09-17
  • 1970-01-01
相关资源
最近更新 更多