问题
I am making google chrome extension project, where i need to close print preview window from my jquery code. Print preview window open automatically when i call site option from my google chrome extension project.
I have also try with --disable-print-preview. in this case open print popup. so if anyone have idea to close print popup with jquery then it's also suitable for me.
回答1:
[ I have found the solution to cancel the browser print preview screen. here is the solution provided in Java by "Erçin Akçay"
// Choosing the second window which is the print dialog.
// Switching to opened window of print dialog.
driver.switchTo().window(driver.getWindowHandles().toArray()[1].toString());
// Run 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());
回答2:
You just need to disable the print preview using given line below. You can use before clicking on the print button.
((JavascriptExecutor)driver).executeScript("window.print=function(){};");
来源:https://stackoverflow.com/questions/47885424/close-print-preview-window-from-google-chrome-extension-project-using-jquery