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

Java 如何告诉Selenium在打印弹出窗口上按“取消”?,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我正在使用Selenium检查页面是否显示。但是,当我单击页面时,会出现打印机打印提示(如显示“选择打印机”之类的窗口)。如何让Selenium通过单击“取消”关闭此窗口 我试着查看警报,但这些警报似乎不起作用,因为打印窗口是一个系统提示。它无法识别出现的任何警报 我最近尝试使用的一种方法是,只发送tab和enter等键,以便选择cancel按钮,但是,它无法识别是否有任何键被按下 我怎么处理这个案子 public static boolean printButton() throws Exce

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

我试着查看警报,但这些警报似乎不起作用,因为打印窗口是一个系统提示。它无法识别出现的任何警报

我最近尝试使用的一种方法是,只发送tab和enter等键,以便选择cancel按钮,但是,它无法识别是否有任何键被按下

我怎么处理这个案子

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;

     }  

实际上,您无法在SeleniumWebDriver中处理windows(OS)对话框。 这就是selenium团队的答案

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


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

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

参考:

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

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

如果你打算只测试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();");

我们也可以使用按键处理打印或按取消按钮操作。这对我很有用

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

我选择这个答案而不是另一个,因为这个答案不需要我下载任何额外的工具。多亏了youHi@florent-b,您能帮我解决以下问题吗:这在python:browser中对我不起作用。执行脚本(“document.querySelector(\'print preview app\”)。shadowRoot.querySelector(\'print preview header\”)。shadowRoot.querySelector(\'paper button.cancel button\”)。单击()我收到一个错误:JavascriptException(“javascript错误:无法读取null的属性'shadowRoot'(会话信息:chrome=75.0.3770.142)\n(驱动程序信息:chromedriver=74.0.3729.6)(255758ECCF3D24491B8A1317AA76E1CE10D57E9引用/分支头/3729}),平台=Windows NT 10.0.17763 x8664)”,无,无) JavascriptException@MacMarde检查chrome 75的解决方案在chrome 79中,这是打印对话框中取消按钮的定位器:返回文档。querySelector('print-preview-app')。shadowRoot。querySelector('print-preview-sidebar')。shadowRoot。querySelector('print-preview-button-strip')。shadowRoot.querySelector('cr-button.cancel button')