You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用TestNG进行Selenium跨浏览器测试时IE浏览器无法关闭求助

Selenium + TestNG 跨浏览器测试中IE浏览器无法关闭的问题解决

我看到你在使用TestNG进行Selenium跨浏览器测试时遇到了IE浏览器无法正常关闭的问题,执行CloseBrowser方法时抛出了NoSuchWindowException,而Chrome浏览器可以正常关闭。下面我们来分析原因并给出具体的解决方案:

问题重现

执行测试后IE浏览器无法关闭,抛出异常:

org.openqa.selenium.NoSuchWindowException: Unable to get browser
Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T19:05:14.666Z'
System info: host: '####', ip: '####', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_144'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities {acceptInsecureCerts: false, browserName: internet explorer, browserVersion: 11, javascriptEnabled: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), se:ieOptions: {browserAttachTimeout: 0, elementScrollBehavior: 0, enablePersistentHover: true, ie.browserCommandLineSwitches: , ie.ensureCleanSession: false, ie.fileUploadDialogTimeout: 3000, ie.forceCreateProcessApi: false, ignoreProtectedModeSettings: false, ignoreZoomSetting: false, initialBrowserUrl: http://localhost:25599/, nativeEvents: true, requireWindowFocus: false}, setWindowRect: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}}
Session ID: e346d8bb-4875-4567-902e-428e73f34013
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

你的测试代码:

package learningTestNGVideo;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class CrossBrowserTesting {
 //global variable WebDriver
 public WebDriver driver;
 @Parameters("browser")
 @BeforeClass
 public void LaunchBrowser(String browser){
 if(browser.equalsIgnoreCase("IE")){
 System.setProperty("webdriver.ie.driver","E:\\Selenium2018\\IEDriverServer_x64_3.8.0\\IEDriverServer.exe");
 driver= new InternetExplorerDriver();
 } else if(browser.equalsIgnoreCase("Chrome")){
 System.setProperty("webdriver.chrome.driver","E:\\Selenium2018\\setup\\chromedriver_win32\\chromedriver.exe");
 driver= new ChromeDriver();
 } else if(browser.equalsIgnoreCase("firefox")){
 System.setProperty("webdriver.gecko.driver","E:\\Selenium2018\\geckodriver-v0.19.1-win64\\geckodriver.exe");
 driver= new FirefoxDriver();
 }
 driver.get("http://automationpractice.com/index.php");
 }
 @Test
 public void ClickonSignIn(){
 driver.findElement(By.xpath(".//*[@id='header']/div[2]/div/div/nav/div[1]/a")).click();
 System.out.println("Successfull");
 }
 @AfterMethod
 public void CloseBrowser() throws IOException{
 driver.close();
 }
}

TestNG配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite1" parallel="tests">
 <test name="Test-IE">
 <parameter name="browser" value="IE"></parameter>
 <classes>
 <class name="learningTestNGVideo.CrossBrowserTesting" />
 </classes>
 </test>
 <!-- Test -->
 <test name="Test-Firefox">
 <parameter name="browser" value="firefox"></parameter>
 <classes>
 <class name="learningTestNGVideo.CrossBrowserTesting" />
 </classes>
 </test>
 <test name="Test-Chrome">
 <parameter name="browser" value="chrome"></parameter>
 <classes>
 <class name="learningTestNGVideo.CrossBrowserTesting" />
 </classes>
 </test>
</suite>
<!-- Suite -->

问题原因分析

  1. driver.close()的局限性:close()仅关闭当前活动窗口,如果IE会话中窗口句柄丢失或者存在多个窗口,就会抛出NoSuchWindowException。
  2. IE驱动配置缺失:IE驱动对浏览器的保护模式、缩放设置、窗口焦点等参数非常敏感,默认配置下容易出现会话异常。
  3. 版本兼容性问题:IEDriverServer版本、Selenium版本和IE浏览器版本不匹配也可能导致此类问题。

解决方案

1. 替换driver.close()为driver.quit()

driver.quit()会彻底终止整个浏览器会话,关闭所有关联窗口并释放驱动进程,比close()更适合测试结束时的资源清理。同时添加空指针判断,避免会话已失效时抛出异常:

@AfterMethod
public void CloseBrowser() throws IOException{
    if(driver != null){
        driver.quit();
    }
}

2. 配置IE驱动的DesiredCapabilities参数

为IE驱动添加必要的配置,解决保护模式、窗口焦点等问题:

@Parameters("browser")
@BeforeClass
public void LaunchBrowser(String browser){
    if(browser.equalsIgnoreCase("IE")){
        System.setProperty("webdriver.ie.driver","E:\\Selenium2018\\IEDriverServer_x64_3.8.0\\IEDriverServer.exe");
        
        DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
        // 忽略保护模式设置(建议保持所有区域保护模式一致,或开启此配置)
        caps.setCapability(InternetExplorerDriver.IGNORE_PROTECTED_MODE_SETTINGS, true);
        // 忽略浏览器缩放设置(需确保IE缩放为100%)
        caps.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
        // 强制获取窗口焦点,避免IE在后台运行导致会话丢失
        caps.setCapability(InternetExplorerDriver.REQUIRE_WINDOW_FOCUS, true);
        // 确保每次启动IE时清除会话缓存
        caps.setCapability(InternetExplorerDriver.ENSURE_CLEAN_SESSION, true);
        
        driver= new InternetExplorerDriver(caps);
    } else if(browser.equalsIgnoreCase("Chrome")){
        System.setProperty("webdriver.chrome.driver","E:\\Selenium2018\\setup\\chromedriver_win32\\chromedriver.exe");
        driver= new ChromeDriver();
    } else if(browser.equalsIgnoreCase("firefox")){
        System.setProperty("webdriver.gecko.driver","E:\\Selenium2018\\geckodriver-v0.19.1-win64\\geckodriver.exe");
        driver= new FirefoxDriver();
    }
    driver.get("http://automationpractice.com/index.php");
}

3. 确保版本兼容性

确认以下版本匹配:

  • Selenium版本(3.8.1)和IEDriverServer版本(3.8.0)尽量保持一致
  • IEDriverServer的架构(x64)和你的Windows系统(amd64)匹配
  • IE浏览器版本(11)和IEDriverServer版本兼容

4. 手动调整IE浏览器设置

打开IE浏览器,进入Internet选项 -> 安全标签页,确保所有区域(Internet、本地Intranet、受信任的站点、受限制的站点)的启用保护模式复选框状态一致(全部勾选或全部取消勾选)。

内容的提问来源于stack exchange,提问作者karishma yadav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:09:58