1

I'm trying to cancel out of the print dialog for Firefox browser using selenium java and unable to find an answer to this question. I have looked into Firefox capabilities and did not find anything related to print preview. I have also tried using SendKeys with window handles and it failed and did not work. Im all out of ideas

Any help would be greatly appreciated.

firefox print dialog

3
  • Have you already tried "print.always_print_silent" parameter? Commented Nov 22, 2024 at 3:25
  • I have not. Do you have an example of the use case? I can give it a try Commented Nov 22, 2024 at 16:17
  • I need to add some tests on the page that shows up before the print preview dialog is displayed because that page was showing up as blank. It does not print it just sits there waiting for a response and I cannot click on buttons using selenium Commented Nov 22, 2024 at 18:30

1 Answer 1

1

I can think of two ways to achieve this.

Use print.always_print_silent flag

WebDriverManager.firefoxdriver().setup();
FirefoxOptions options = new FirefoxOptions();
options.addPreference("print.always_print_silent", true);
options.addPreference("print.printer_Microsoft_Print_to_PDF.print_to_file", true);
WebDriver driver = new FirefoxDriver(options);
driver.get("http://localhost:3000");
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
jsExecutor.executeScript("window.print();");

Another way can be to use TakeScreenshot from selenium.

File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

First get width and height

JavascriptExecutor js = (JavascriptExecutor) driver;
int totalHeight = ((Number) js.executeScript("return document.body.scrollHeight")).intValue();
int viewportHeight = ((Number) js.executeScript("return window.innerHeight")).intValue();

use javascript to keep scrolling and keep taking snaps.

int scrollPosition = 0;
List<File> screenshots = new ArrayList<>();
while (scrollPosition < totalHeight) {
    File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    screenshots.add(screenshot);
    scrollPosition += viewportHeight;
    js.executeScript("window.scrollBy(0, arguments[0]);", viewportHeight);
    Thread.sleep(1000); // sleep to handle load time
}

This way you will have the snaps in form of PNG, you can then use any PDF library (e.g. PDFBox) to add the images to pdf and save it as a file.

You can refer Java: Create PDF pages from images using PDFBox 1 library for the task.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you the "silent printing" did the job the print save dialog is annoying but not a deal breaker the test is still able to continue and pass. I will take that as acceptable.
So I enhanced your code. I found other user who posted additional preferences. So if you add this 2nd preference"MS_Print_to_PDF" it will hide the save file dialog box: FirefoxOptions ffOptions = new FirefoxOptions(); ffOptions.addPreference("print.always_print_silent", true); ffOptions.addPreference("print.printer_Microsoft_Print_to_PDF.print_to_file", true); driver = new FirefoxDriver(ffOptions); Thank again
@user3329818 Did this answer your question? If so accept the answer.
I will update the answer to cover the additional details which @user3329818 mentioned for someone who comes later with the same problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.