java 如何告诉 Selenium 在打印弹出窗口上按取消?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36631628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I tell Selenium to press cancel on a print popup?
提问by J. Doe
I am checking whether or not a page appears using Selenium. When I click the page, however, a printer print prompt appears (like the window that says select printer and such). How can I have Selenium close this window by hitting cancel?
我正在检查页面是否使用 Selenium 出现。但是,当我单击该页面时,会出现打印机打印提示(如显示选择打印机等的窗口)。如何通过点击取消让 Selenium 关闭此窗口?
I tried looking to alerts, but it seems like those will not work since the print window is a system prompt. It does not recognize any alerts appearing.
我尝试查看警报,但由于打印窗口是系统提示,因此这些警报似乎不起作用。它无法识别出现的任何警报。
The most recent I tried using is by just sending keys like tab and enter in order to have the cancel button selected, however, it doesn't recognize any keys as being pressed.
我最近尝试使用的方法是仅发送 Tab 和 Enter 等键以选择取消按钮,但是,它无法识别任何被按下的键。
How can I handle this case?
我该如何处理这个案子?
public static boolean printButton() throws Exception {
WebDriver driver = new FirefoxDriver();
driver.get("website");
try {
Thread.sleep(3000);
WebElement temp = driver.findElement(By.xpath("//*[@id='block-print-ui-print-links']/div/span/a"));
temp.click();
Actions action = new Actions(driver);
action.sendKeys(Keys.TAB).sendKeys(Keys.ENTER);
Thread.sleep(6000);
}
catch (Exception e) {
System.out.println("No button.");
driver.close();
return false;
}
回答by Florent B.
I would simply disable the print dialog by overriding the print method :
我会简单地通过覆盖打印方法来禁用打印对话框:
((JavascriptExecutor)driver).executeScript("window.print=function(){};");
But if you goal is to test that the printing is called then :
但是,如果您的目标是测试是否调用了打印,则:
// get the print button
WebElement print_button = driver.findElement(By.cssSelector("..."));
// click on the print button and wait for print to be called
driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS);
((JavascriptExecutor)driver).executeAsyncScript(
"var callback = arguments[1];" +
"window.print = function(){callback();};" +
"arguments[0].click();"
, print_button);
回答by Er?in Ak?ay
If you are going for testing only Chrome browser here is mine solution. Because of 'Robot' class or disabling print didn't work for my case.
如果您只打算测试 Chrome 浏览器,这里是我的解决方案。由于“机器人”类或禁用打印对我的情况不起作用。
// Choosing the second window which is the print dialog.
// Switching to opened window of print dialog.
driver.switchTo().window(driver.getWindowHandles().toArray()[1].toString());
// Runs javascript code for cancelling print operation.
// This code only executes for Chrome browsers.
JavascriptExecutor executor = (JavascriptExecutor) driver.getWebDriver();
executor.executeScript("document.getElementsByClassName('cancel')[0].click();");
// Switches to main window after print dialog operation.
driver.switchTo().window(driver.getWindowHandles().toArray()[0].toString());
Edit: In Chrome 71 this doesn't seem to work anymore since the script can't find the Cancel button. I could make it work by changing the line to:
编辑:在 Chrome 71 中,这似乎不再起作用,因为脚本找不到取消按钮。我可以通过将行更改为:
executor.executeScript("document.querySelector(\"print-preview-app\").shadowRoot.querySelector(\"print-preview-header\").shadowRoot.querySelector(\"paper-button.cancel-button\").click();");
回答by Eugene
Actually you can't handle windows (OS) dialogs inside Selenium WebDriver. This what the selenium team answers here
实际上,您无法在 Selenium WebDriver 中处理 Windows (OS) 对话框。这就是硒团队在这里回答的
The current team position is that the print dialog is out of scope for the project. WebDriver/Selenium is focused on emulating a user's interaction with the rendered content of a web page. Other aspects of the browser including, but not limited to print dialogs, save dialogs, and browser chrome, are all out of scope.
当前团队的立场是打印对话框超出了项目的范围。WebDriver/Selenium 专注于模拟用户与网页呈现内容的交互。浏览器的其他方面,包括但不限于打印对话框、保存对话框和浏览器镶边,都超出了范围。
You can try different approach like AutoIt
您可以尝试不同的方法,例如AutoIt
回答by Raghu K Nair
Native window based dialog can be handled by AutoItX as described in the following code
AutoItX 可以处理基于本机窗口的对话框,如下面的代码所述
File file = new File("lib", jacobDllVersionToUse);
System.setProperty(LibraryLoader.JACOB_DLL_PATH, file.getAbsolutePath());
WebDriver driver = new FirefoxDriver();
driver.get("http://www.joecolantonio.com/SeleniumTestPage.html");
WebElement printButton = driver.findElement(By.id("printButton"));
printButton.click();
AutoItX x = new AutoItX();
x.winActivate("Print");
x.winWaitActive("Print");
x.controlClick("Print", "", "1058");
x.ControlSetText("Print", "", "1153", "50");
Thread.sleep(3000); //This was added just so you could see that the values did change.
x.controlClick("Print", "", "2");
Reference : http://www.joecolantonio.com/2014/07/21/selenium-how-to-handle-windows-based-dialogs-and-pop-ups/
参考:http: //www.joecolantonio.com/2014/07/21/selenium-how-to-handle-windows-based-dialogs-and-pop-ups/
回答by akanshu
we can also use key for handling the print or press the cancel button operation. and it works for me.
我们也可以使用按键来处理打印或按取消键操作。它对我有用。
driver.switchTo().window(driver.getWindowHandles().toArray()[1].toString());
WebElement webElement = driver.findElement(By.tagName("body"));
webElement.sendKeys(Keys.TAB);
webElement.sendKeys(Keys.ENTER);
driver.switchTo().window(driver.getWindowHandles().toArray()[0].toString());