Search code examples
c#selenium-webdrivermicrosoft-edge

Why is Selenium hung when accessing the URL?


I am a beginner of Selenium.Now I have a website must access by IE (Chrome or Edge do not support it at all).

As Microsoft remove IE in the newest Windows, the website can only access by Compatibility (IE) Mode in Edge.

Here is my code:

 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);

After the program ran, it opens Edge browser and accesses the homepage of the website by IE mode successfully.

However, even I add a breakpoint to the line Debug.WriteLine(driver. Title); it never runs.

After 1 minute, it reports an error like this:

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.

What's wrong with it?


Solution

  • The right way to automate IE mode is defining InternetExplorerOptions with additional properties that point to the Edge browser. You need to set ieOptions.AttachToEdgeChrome property to true, and ieOptions.EdgeExecutablePath to the path of the Microsoft Edge executable.

    Please refer to the following steps:

    1. Download IEDriver and make sure that the version of IEDriver that you download is 4.0.0.0 or greater.
    2. Meet the requirements for Selenium's required configuration.
    3. Place the IEDriver executable in your PATH.
    4. Sample code to automate IE mode:
        using System;
        using OpenQA.Selenium;
        using OpenQA.Selenium.IE;
        
        namespace IEDriverSample
        {
            class Program
            {
                static void Main(string[] args)
                {
                    var ieOptions = new InternetExplorerOptions();
                    ieOptions.AttachToEdgeChrome = true;
                    //change the path accordingly
                    ieOptions.EdgeExecutablePath = "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe";
        
                    var driver = new InternetExplorerDriver(ieOptions);
                    driver.Url = "http://cced.com/cced.aspx";
        
                    driver.Quit();
                }
            }
        }
    

    You can also refer to this official doc for detailed information: Use Internet Explorer Driver to automate IE mode in Microsoft Edge.


    Update:

    1. If Protected Mode doesn't exist in IE setting, you need to check it in registry. Please check HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones in registry, under Zones is a subkey for each zone:
    • 1 (Intranet zone)
    • 2 (Trusted Sites zone)
    • 3 (Internet zone)
    • 4 (Restricted Sites zone)

    Under each numerically-named key, you can check or create a REG_DWORD value named 2500 to control Protected Mode for each zone. Setting that value to 0 enables Protected Mode; a setting of 3 disables it.

    1. It's recommended to download and use 32-bit IEDriver. In my experience, there might be some issues when using 64-bit version.