0

I'm having issues with running chrome driver in headless mode and interacting with Print Dialog box. It seems that headless chrome doesn't even open Print Dialog Box.

Here is my code for clicking on Save As Pdf button

    private static void clickOnSaveAsPdf() {
        WebElement saveAsPdfButton = chromeDriver.findElement(By.xpath("//*[@id=\"root\"]/div/div[3]/div[1]/main/div/div/div[1]/div[3]/div/button[2]"));
        saveAsPdfButton.click();
    }

This part of code above is executed without issues every time. So the issues start when i try to switch windows and focus on print dialog box. When I execute this line of code:

System.out.println(chromeDriver.getWindowHandles());

I get different results when in headless and when in non-headless.

In non-headless mode result I'm getting is: [CDwindow-55CA15B96D8DB58A454CC30B30F0F970, CDwindow-8CB30AB28BC2E8BCDE395BAA9F7B9101]

But in headless mode, result I'm getting is: [CDwindow-15ED0AA78CE73CA53CA71E6FB2A94998]

So, obviously, headless chrome doesn't see print dialog box. Is there any way I can access it? Here is the method that starts ChromeDriver, with its options and prefs:

    public static void runChromeDriver() {
        String chromeDriverPath = "/home/nemanja/Downloads/chromedriver_linux64/chromedriver";

        System.setProperty("webdriver.chrome.driver", chromeDriverPath);

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless");
        options.addArguments("--window-size=1920, 1080");
        options.addArguments("--ignore-certificate-errors");

        HashMap<String, Object> prefs = new HashMap<String, Object>();

        prefs.put("profile.default_content_settings.popups", 0);
        prefs.put("download.default_directory", getFullPathToTempDirectory());
        prefs.put("download.prompt_for_download", false);
        prefs.put("download.directory_upgrade", true);
        prefs.put("plugins.always_open_pdf_externally", true);

        options.setExperimentalOption("prefs", prefs);

        chromeDriver = new ChromeDriver(options);
    }

Some further info, I'm using Java 17, ChromeDriver v111 and my OS is Ubuntu 22.04.

bici
  • 47
  • 1
  • 8

1 Answers1

0

Before you invoke getWindowHandles() ensure that the second window handle is present inducing WebDriverWait for numberOfWindowsToBe(n) as follows:

new WebDriverWait(driver, Duration.ofSeconds(5)).until(ExpectedConditions.numberOfWindowsToBe(2));
System.out.println(chromeDriver.getWindowHandles());
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Hi and thanks for the reply, I added that line to wait (for 10 seconds), but I'm getting the exception, ```Expected condition failed: waiting for number of open windows to be 2``` – bici Feb 13 '23 at 09:08
  • So second window isn't opened and the two window handles you observe in non-headless mode is something else. – undetected Selenium Feb 13 '23 at 10:11
  • Second window is the print window in non-headless mode, i can interact with it. But in the headless mode it just isn't there even though the button is clicked, or its there but chromeDriver just doesn't see it. – bici Feb 13 '23 at 11:48