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

前端 未结 5 1817
猫巷女王i
猫巷女王i 2020-12-06 08:17

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

5条回答
  •  半阙折子戏
    2020-12-06 08:21

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

提交回复
热议问题