如何告诉Selenium在打印弹出窗口上按“取消"? [英] How can I tell Selenium to press cancel on a print popup?

查看:748
本文介绍了如何告诉Selenium在打印弹出窗口上按“取消"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我尝试寻找警报,但是由于打印窗口是系统提示,因此似乎无法使用.它无法识别出现的任何警报.

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.

我最近尝试使用的方法是仅发送选项卡(如tab)并按Enter才能选择取消按钮,但是,它无法识别出任何按下的键.

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.

如何处理这种情况?

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;

     }  

推荐答案

我将通过覆盖打印方法来简单地禁用打印对话框:

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

这篇关于如何告诉Selenium在打印弹出窗口上按“取消"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆