繁体   English   中英

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

[英]Selenium chromedriver losing window handles. org.openqa.selenium.NoSuchWindowException: no such window error

我正在使用以下内容,

Chromedriver 2.41

硒的服务器独立,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

我也遇到过同样的问题。 使用Thread.sleep(500);花费一些时间来获取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));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM