我目前正在使用 selenium Web 驱动程序进行自动化功能测试。我遇到了一个场景,我需要单击窗口(例如 A)上的打印按钮来打开一个新窗口(例如 B)。窗口 B 也弹出了打印对话框。我需要使用 java 脚本执行器关闭该打印对话框。
这个我已经试过了。但它不起作用。我的代码:
public void handlePrintButtonScenario {
String parentWindow_A = driver.getWindowHandle(); //get the current window A handle
Printbutton().click(); //clicking print button
//switch focus of web driver to newly opened window B
for (String winhandle : driver.getWindowHandle()) {
driver.switchTo().window(winhandle);
}
//closing printdialoge
closeWindowByJS();
driver.close();// closing window B
driver.switchTo().window(parentWindow_A); // switching focus to window A
}
public void closeWindowByJS() {
JavascriptExecutor js = (JavascriptExecutor) driver;
String script = "window.onbeforeunload = null;" + "window.close();";
js.executeScript(script);
}
现在,当我第一次通过调用 closeWindowByJS() 方法执行该场景时,发生了打印对话和窗口 B 关闭的情况。我在 driver.close() 行中遇到异常,说明没有找到这样的窗口。
但是当我重新运行该场景时,即从第二次开始,打印对话和窗口 B 都没有关闭。但焦点得到了正确的改变,场景也正常运行。
但是我需要首先使用 javascriptexecutor 关闭打印对话框,然后关闭窗口 B。我不需要通过调用 closeWindowByJS() 来关闭这两个窗口。之后需要将焦点切换到窗口 A。
请帮忙?
最佳答案
使用自动功能 下载exe记录点击按钮并记录代码 使用相同的java脚本执行器执行autoit的代码
你的driver.close() 应该在switch window()代码之后
这应该有帮助
关于java - 在 selenium Web 驱动程序 firefox 中使用 javascriptexecutor 单击打印按钮时无法关闭打印对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25770652/