Selenium chromedriver丢失窗口句柄。 org.openqa.selenium.NoSuchWindowException:没有此类窗口错误

时间:2018-10-03 09:37:13

标签: java selenium selenium-chromedriver

我正在使用以下内容,

Chromedriver 2.41

selenium-server-standalone-3.10.0.jar

jdk 1.8.0_141

我使用硒打开chrome中的多个标签,一个一个地遍历每个标签,然后执行一些脚本。但是,在切换窗口句柄时,我经常会出错。刚要在切换窗口之前使用System.out.println(driver.getWindowHandles()),我发现窗口句柄的数量随着时间的推移而不断减少,并且得出org.openqa.selenium.NoSuchWindowException: no such window

另一个奇怪的是,代码将运行一次。然后它将为以后的测试提供错误,但是如果我们将其保留十分钟,它将再次正常运行。

代码实现如下,

        ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
        for(int j=0; j<10; j++){
            ((JavascriptExecutor) driver).executeScript("window.open('" + *siteURL* + "')");
            for(String temp : driver.getWindowHandles()){
                if(!tabs.contains(temp)){
                    driver.switchTo().window(temp);
                    tabs.add(temp);
                }
            }
        }

        tabs = new ArrayList<String>(driver.getWindowHandles());
        driver.switchTo().window(tabs.get(0));

        /*** Reload tabs to reacquire the pages- Selenium loses them if it is idle for long time
         * **/
            for (int j = 0; j < tabs.size(); j++) {
                driver.switchTo().window(tabs.get(j));
                driver.get(*siteURL*);

            }

        tabs = new ArrayList<String>(driver.getWindowHandles());
        for (int j = 0; j < tabs.size(); j++) {
          driver.switchTo().window(tabs.get(j));
         /*Perform step 1*/
        }

错误:

org.openqa.selenium.NoSuchWindowException: no such window


(Session info: chrome=67.0.3396.87)
  (Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.10.0', revision: '176b4a9', time: '2018-03-02T19:11:12.151Z'
System info: host: 'APACSGAWSBU0130', ip: '10.58.196.139', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_141'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.41.578737 (49da6702b16031..., userDataDir: C:\Users\HY84957\Downloads\...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:54006}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 67.0.3396.87, webStorageEnabled: true}
Session ID: 0f98cc640eea21f2c48120f3bb901846

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。 getWindowHandles()需要一些时间,Thread.sleep(500);可以正常工作。

public void goto2() throws InterruptedException {
    ArrayList<String> winHandles = new ArrayList<String> (driver.getWindowHandles());
    Thread.sleep(500);
    driver.switchTo().window(winHandles.get(1));
}