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.