使用selenium webdriver按Alt + F4键

时间:2014-08-11 18:08:08

标签: selenium-webdriver

我使用selenium webdriver自动化应用程序。我的应用程序中有一个模态窗口,我们无法执行任何操作或处理它。我们只需关闭窗口。我们唯一的选择是按Alt + F4键关闭窗口。我有以下代码来执行那些无效的操作。请让我知道如何实现这个

代码:

Actions actions = new Actions(driver); actions.keyDown(Keys.ALT); actions.sendKeys(Keys.F4); actions.keyUp(Keys.ALT); actions.perform();

3 个答案:

答案 0 :(得分:0)

您可以使用以下代码

实现相同目的
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_F4);

要运行此代码,您需要添加,     import java.awt.Robot;

答案 1 :(得分:0)

您可以使用“driver.close”。它将关闭当前活动窗口。

答案 2 :(得分:0)

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

希望它适合你:)