Selenium调用Edge兼容IE模式访问URL时卡顿超时问题求助
问题:Edge IE模式下Selenium IEDriver超时无法获取页面内容
我是Selenium新手,目前要操作一个仅支持IE的网站(Chrome/Edge完全不兼容)。新版Windows已移除IE,只能通过Edge的IE兼容模式访问。
我的代码如下:
var options = new InternetExplorerOptions { IgnoreZoomLevel = true, IntroduceInstabilityByIgnoringProtectedModeSettings = true, EnsureCleanSession = true, PageLoadStrategy = PageLoadStrategy.Normal }; IWebDriver driver = new InternetExplorerDriver($@"{AppDomain.CurrentDomain.BaseDirectory}\IEDriverServer.exe", options); driver.Manage().Window.Maximize(); driver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(60); driver.Navigate().GoToUrl("http://cced.com/cced.aspx"); Debug.WriteLine(driver.Title);
程序运行后,能成功打开Edge并以IE模式进入网站首页,但Debug.WriteLine(driver.Title);从未执行,1分钟后触发超时错误:
OpenQA.Selenium.WebDriverException HResult=0x80131500 Message=The HTTP request to the remote WebDriver server for URL http://localhost:9324/session/700c0df3-fcf5-459e-a9d9-0487cfd42d15/url timed out after 60 seconds. Source=WebDriver StackTrace: at OpenQA.Selenium.Remote.HttpCommandExecutor.<ExecuteAsync>d__34.MoveNext() at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.<ExecuteAsync>d__10.MoveNext() at OpenQA.Selenium.WebDriver.<ExecuteAsync>d__63.MoveNext() at OpenQA.Selenium.Navigator.<GoToUrlAsync>d__7.MoveNext() at OpenQA.Selenium.Navigator.<>c__DisplayClass6_0.<<GoToUrl>b__0>d.MoveNext() at OpenQA.Selenium.Navigator.GoToUrl(String url) at ERPDownload.Form1.Form1_Load(Object sender, EventArgs e) in D:\Project\Test\Test\Form1.cs:line 28 at System.Windows.Forms.Form.OnLoad(EventArgs e) at System.Windows.Forms.Control.CreateControl(Boolean ignoreVisible) at System.Windows.Forms.Control.CreateControl() at System.Windows.Forms.Control.WmShowWindow(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(HWND hWnd, MessageId msg, WPARAM wparam, LPARAM lparam) Inner Exception 1: TaskCanceledException: The request was canceled due to the configured HttpClient.Timeout of 60 seconds elapsing. Inner Exception 2: TimeoutException: The operation was canceled. Inner Exception 3: TaskCanceledException: The operation was canceled. Inner Exception 4: IOException: Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request. Inner Exception 5: SocketException: The I/O operation has been aborted because of either a thread exit or an application request.
原因分析
- IEDriver与Edge IE模式的兼容性冲突:直接使用
InternetExplorerDriver驱动Edge IE模式时,IEDriver无法正确识别和控制Edge容器内的IE内核实例,导致命令发送后无响应,最终触发超时。 - 页面加载策略不匹配:
PageLoadStrategy.Normal会等待页面完全加载完成才继续执行,但IE模式下页面可能存在加载缓慢或未正确触发加载完成事件的情况,导致驱动一直处于等待状态。 - 保护模式设置无效:虽然配置了
IntroduceInstabilityByIgnoringProtectedModeSettings = true,但Edge IE模式的保护机制与传统IE不同,该设置无法生效,进而影响驱动与浏览器的通信。
解决方法
方法1:改用EdgeDriver配置IE模式(推荐)
直接使用EdgeDriver并强制启用IE模式,替代IEDriver的方式:
var edgeOptions = new EdgeOptions(); // 配置强制IE模式及指定IE版本 edgeOptions.AddAdditionalEdgeOption("ie.edgechromium", true); edgeOptions.AddAdditionalEdgeOption("ie.emulationMode", "IE11"); // 指定Edge安装路径(根据实际路径调整) edgeOptions.AddAdditionalEdgeOption("ie.edgepath", @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"); IWebDriver driver = new EdgeDriver(edgeOptions); driver.Manage().Window.Maximize(); // 延长页面加载超时时间 driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(120); driver.Navigate().GoToUrl("http://cced.com/cced.aspx"); Debug.WriteLine(driver.Title);
方法2:调整IEDriver参数
如果坚持使用IEDriver,尝试以下修改:
- 将页面加载策略改为
Eager,等待DOM加载完成即继续执行,无需等待所有资源加载:PageLoadStrategy = PageLoadStrategy.Eager - 延长页面加载超时时间:
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(120); - 确保IEDriverServer版本与系统IE核心组件版本匹配(Edge IE模式依赖系统IE组件)。
方法3:检查系统与Edge配置
- 确保Windows已安装IE模式兼容性组件(Windows 11需手动安装:设置→应用→可选功能→添加功能→搜索“Internet Explorer模式”并安装)。
- 在Edge设置中,将目标网站添加到IE模式站点列表,确保网站默认以IE模式打开。
内容的提问来源于stack exchange,提问作者Melon NG
相关产品推荐
相关产品推荐

