如何告诉Selenium在打印弹出窗口上按取消?

时间:2016-04-14 18:53:49

标签: java selenium selenium-webdriver

我正在检查是否使用Selenium显示页面。但是,当我单击该页面时,会出现打印机打印提示(如选择打印机等的窗口)。如何通过点击取消让Selenium关闭此窗口?

我试图寻找警报,但似乎那些不起作用,因为打印窗口是系统提示。它无法识别出现的任何警报。

我尝试使用的最新版本只是发送tab之类的键并输入以便选择取消按钮,但是,它无法识别任何键被按下。

我该如何处理这个案子?

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;

     }  

6 个答案:

答案 0 :(得分:11)

我只是通过覆盖print方法来禁用打印对话框:

((JavascriptExecutor)driver).executeScript("window.print=function(){};");

但如果您的目标是测试打印是否被调用,那么:

// 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);

答案 1 :(得分:4)

如果您只想测试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());

修改:在Chrome 71中,由于脚本无法找到“取消”按钮,因此似乎不再起作用。我可以通过将行更改为:

来使其工作
executor.executeScript("document.querySelector(\"print-preview-app\").shadowRoot.querySelector(\"print-preview-header\").shadowRoot.querySelector(\"paper-button.cancel-button\").click();");

答案 2 :(得分:3)

实际上你无法处理Selenium WebDriver中的窗口(OS)对话框。 这就是硒团队回答的问题here

  

当前团队的立场是打印对话框超出范围   该项目。 WebDriver / Selenium专注于模拟用户   与呈现的网页内容的交互。其他方面   浏览器包括但不限于打印对话框,保存对话框,   和浏览器chrome,都超出了范围。

您可以尝试不同的方法,例如AutoIt

答案 3 :(得分:1)

基于原生窗口的对话框可由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");

参考:http://www.joecolantonio.com/2014/07/21/selenium-how-to-handle-windows-based-dialogs-and-pop-ups/

答案 4 :(得分:1)

我们还可以使用键来处理打印或按取消按钮操作。它对我有用。

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());

答案 5 :(得分:0)

有时 2 个不同的语句如上 (webElement.sendKeys(Keys.TAB) webElement.sendKeys(Keys.ENTER)) 将不起作用,您可以结合使用 Tab 和 Enter 键,如下所示,这将关闭打印预览窗口。

使用 C#:

Driver.SwitchTo().Window(Driver.WindowHandles[1]);
IWebElement element = Driver.FindElement(By.TagName("body"));  
element.SendKeys(Keys.Tab + Keys.Enter);                
Driver.SwitchTo().Window(Driver.WindowHandles[0]);