How can I tell Selenium to press cancel on a print popup?


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.

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.

How can I handle this case?

public static boolean printButton() throws Exception {

    WebDriver driver = new FirefoxDriver();

    try {

        WebElement temp = driver.findElement(By.xpath("//*[@id='block-print-ui-print-links']/div/span/a"));;
        Actions action = new Actions(driver); 



     catch (Exception e) {

        System.out.println("No button.");
        return false;



I would simply disable the print dialog by overriding the print method :


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);
    "var callback = arguments[1];" +
    "window.print = function(){callback();};" +
    , print_button);

Answered By – Florent B.

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published