簡體   English   中英

如何在不使用機械手的情況下關閉打開的文件

[英]How to close an opened file without using robot

我正在使用Selenium測試網站。 我可以上傳“ .txt”文件,然后雙擊打開它,但是我無法使用硒關閉打開的文件!

我知道有一個使用Alt + F4的機器人工具解決方案,但我不允許使用機器人,我嘗試了下面的硒代碼關閉窗口,但它不起作用:

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

試試這個( driverWebDriver的實例):

String myWindow = driver.getWindowHandle();

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

driver.switchTo().window(myWindow);

這將存儲原始窗口的句柄並切換回它。 如果調用driver.quit();另一個窗口可能仍在后台打開,但將關閉driver.quit();

感謝: 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM