Skip to main content
 首页 » 编程设计

c#之Edge 浏览器 WebDriver 故障

2024年05月29日48lyj

尝试从 http://blogs.windows.com/msedgedev/2015/07/23/bringing-automated-testing-to-microsoft-edge-through-webdriver/ 生成代码工作。

得到一个丑陋的异常。

重现步骤。

从提供的链接安装网络驱动程序(2015 年 7 月 24 日 WebDriver) 创建控制台应用程序。 Selenium.WebDriver、Selenium.Support 中的 Nuget。 运行代码,控制台窗口正常显示。

当代码命中 driver.Url="https://www.bing.com"时,它会引发异常,如下所述。

NoSuchWindowException - WebDriver.dll 中发生类型为“OpenQA.Selenium.NoSuchWindowException”的未处理异常

我的代码片段如下:

using System.IO; 
using OpenQA.Selenium; 
using OpenQA.Selenium.Edge; 
using OpenQA.Selenium.Remote; 
using OpenQA.Selenium.Chrome; 
using OpenQA.Selenium.Support.UI; 
namespace WebDriverPlay 
{ 
public class msedgedev_sample 
{ 
    public static void RunMSEdgeDevSample() 
    { 
        Console.WriteLine("running MSEdgeDev Sample"); 
        RemoteWebDriver driver = null; 
        string serverPath = "Microsoft Web Driver"; 
        try 
        { 
            if (System.Environment.Is64BitOperatingSystem) 
            { 
                serverPath = Path.Combine(System.Environment.ExpandEnvironmentVariables("%ProgramFiles(x86)%"), serverPath); 
            } 
            else 
            { 
                serverPath = Path.Combine(System.Environment.ExpandEnvironmentVariables("%ProgramFiles%"), serverPath); 
            } 
 
            // location for MicrosoftWebDriver.exe 
            EdgeOptions options = new EdgeOptions(); 
            options.PageLoadStrategy = EdgePageLoadStrategy.Eager; 
            driver = new EdgeDriver(serverPath, options); 
 
            //Set page load timeout to 5 seconds 
            driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(5)); 
 
            //string _url = @"https://www.bing.com/"; 
            string _url = @"http://www.google.com"; 
            Console.WriteLine("_url=" + _url); 
            driver.Url = _url; 
 
        } 
        catch (Exception e) 
        { 
            Console.WriteLine(e.Message); 
        } 
        finally 
        { 
            if (driver != null) 
            { 
                driver.Close(); 
            } 
        } 
    } 
} 

请您参考如下方法:

行后:

driver = new EdgeDriver(serverPath, options);

执行后,您应该会看到一个命令窗口打开并连接到 Edge。如果 Edge 浏览器已经打开,它将关闭它并打开一个新实例。根据您的错误,我认为您没有看到这种行为,对吗?如果是这样,可能有什么东西阻止了 WebDriver 服务器在本地启动(Defender??)。检查条件设置 serverPath。我无法解析 Is64BitOperatingSystem,因此我选择了正确的路径并删除了其余的条件,将 serverPath 设置为 MicrosoftWebDriver.exe 的位置。

如果路径不正确,它将无法通过“驱动程序”实例化。不知何故,你正在进入 driver.Url 调用,我假设你正在通过该 serverPath 获得一些解决方案。因此,本地设备上的某些内容可能会阻止 MicrosoftWebDriver.exe 运行。

同样,您应该会看到一个命令提示符,其中显示了正确的通信日志记录。

最后一个提示,您可以转到 MicrosoftWebDriver.exe 并运行它。那你可以去:http://dev.modern.ie/testdrive/demos/webdriver/和“发送请求”的默认值,应该是创建一个 session 。您将看到发布到页面的结果,还可以在命令窗口中看到通信记录。

请务必从与 Edge 不同的浏览器转到该页面,因为它会杀死现有的 Edge 窗口,包括它本身。