admin管理员组文章数量:1122846
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.
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.
Share Improve this question asked Nov 21, 2024 at 18:24 user3329818user3329818 411 silver badge7 bronze badges 3- Have you already tried "print.always_print_silent" parameter? – Anubhav Sharma 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 – user3329818 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 – user3329818 Commented Nov 22, 2024 at 18:30
1 Answer
Reset to default 1I 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.
本文标签: How to disabled print preview in Firefox browser with selenium javaStack Overflow
版权声明:本文标题:How to disabled print preview in Firefox browser with selenium java - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736308090a1933539.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论