0
votes

Here is the scenario:

  1. Click a link that opens up to a new tab.
  2. Call getWindowHandles() and switch to the new tab.
  3. Use JavaScript Executor to open up the Print PopUp
  4. Call getWindowHandles() again to switch focus to the Print PopUp

This second getWindowHandles() call is causing my script to timeout and I'm not sure why. Could someone help me figure out what's going on with this second call? Everything is working as expected until step 4

        link.click();
        ArrayList<String> handles2 = new ArrayList<String> (driver.getWindowHandles());
        driver.switchTo().window(handles2.get(1));
        
        ((JavascriptExecutor)driver).executeScript("window.print()");

        ArrayList<String> handles3 = new ArrayList<String> (driver.getWindowHandles());

Maybe useful information:

Error Message:

org.openqa.selenium.ScriptTimeoutException: script timeout (Session info: chrome=86.0.4240.183) Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03' System info: host: '', ip: '', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_261' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities {acceptInsecureCerts: true, browserName: chrome, browserVersion: 86.0.4240.183, chrome: {chromedriverVersion: 86.0.4240.22 (..., userDataDir: C:\Users\AppData\L...}, goog:chromeOptions: {debuggerAddress: localhost:}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true}

1
Depending on the site and how it prints, I don't think that the print dialog is considered a window handle in Selenium terms. It's actually a browser dialog. A window handle in Selenium terms is a browser window/tab. I don't know what youare window.print() line does... does it open a new browser window (I'm assuming not) or launch the browser print dialog (I'm assuming yes)?JeffC
It may be blocking once the print dialog is open. (So the browser can't do anything until that window is closed.)pcalkins
@JeffC it does the latter, opens the browser print dialog. The research I've done online, shows that you can switch to the print dialog like so. That could have changed in the version of Chrome I have.flyaboveothers
@pcalkins I want to do some work on the print dialog, so I want to keep that part open.flyaboveothers
if it is blocking you may need to use Robot for this: docs.oracle.com/javase/7/docs/api/java/awt/Robot.htmlpcalkins

1 Answers

0
votes

The issue was the print dialog causing the main thread to lock out.

You are able to use the Robot class as suggested by @pcalkins. But I used the SetTimeout function.

Reference: ChromeDriver - Timeout when trying to print page with javascript