0

我在 C# 中使用 Selenium 和 Edge 驱动程序,但是当我运行代码时,我收到以下错误。使用 OpenQA.Selenium;使用 OpenQA.Selenium.Edge;

class HomePageTests
{
    static void Main(string[] args)
    {
        {
            {
                IWebDriver AzimaHome = new EdgeDriver();
                AzimaHome.Navigate().GoToUrl("http:www.msn.com");

                IList<IWebElement> terms = AzimaHome.FindElements(By.TagName("a"));
                terms.First(element => element.Text == "").Click();
            }
        }
    }
}

抛出异常:

OpenQA SeleniumWebDriverException' in WebDriver.dll
An unhandled exception of type SeleniumWebDriverException' occurred in WebDriverdll
A exception with a null response was thrown sending an HTTP request to the remote WebDriver server for URL http://localhost:52586/session. The status of the exception was Receive Failure, and the message was: The underlying connection was closed: An unexpected error occurred on a receive.

顺便说一句,这是在 Visual Studio 2019 中:

_05.NoSuchElementException.exe' (CLR v4.0.30319: _05.NoSuchElementException.exe): Loaded 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\Private Assemblies\Runtime\Microsoft.VisualStudio.Debugger.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The program '[17312] _05.NoSuchElementException.exe' has exited with code -1 (0xffffffff).

有人可以帮忙吗?

4

2 回答 2

0

此错误消息...

OpenQA SeleniumWebDriverException' in WebDriver.dll
An unhandled exception of type SeleniumWebDriverException' occurred in WebDriverdll
A exception with a null response was thrown sending an HTTP request to the remote WebDriver server for URL http://localhost:52586/session. The status of the exception was Receive Failure, and the message was: The underlying connection was closed: An unexpected error occurred on a receive.

...意味着EdgeDriver无法启动/产生新的浏览上下文,即Microsoft Edge 浏览器会话。

有关您的Microsoft EdgeMicrosoft EdgeHTML的更多详细信息将有助于我们以更好的方式调试问题。但是,如果您使用EdgeHTML 18or EdgeHTML 19,根据Microsoft WebDriver中的文档:

适用于 Microsoft Edge (EdgeHTML) 版本 18 和 19 的 Microsoft WebDriver 是一项 Windows 按需功能,可确保它始终自动保持最新状态,并支持一些获取 Microsoft WebDriver 的新方法。


脚步

要进行配置,您必须启用开发人员模式:

Go to Settings > Update and Security > For Developer and then select "Developer mode".

要通过提升的命令提示符安装运行Microsoft Edge 版本 18 :

DISM.exe /Online /Add-Capability /CapabilityName:Microsoft.WebDriver~~~~0.0.1.0

tl; 博士

根据Microsoft Edge 开发人员指南

EdgeHTML 18 包括 Microsoft Edge 平台的当前版本中提供的以下新功能和更新功能,截至2018 年 10 月 10 日的 Windows 更新(10/2018,内部版本 17763)。有关特定Windows Insider Preview 版本的更改,请参阅Microsoft Edge 更改日志EdgeHTML 中的新增功能

于 2019-12-23T21:35:01.427 回答
0

除了最后一行之外,您的大部分代码都是正确的。我对其进行了修改并使用以下代码进行了测试,它可以正常工作:

using System.Collections.Generic;
using System.Linq;
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;

public class webdriver
{
    static void Main(string[] args)
    {    
        IWebDriver AzimaHome = new EdgeDriver();
        AzimaHome.Navigate().GoToUrl("http:www.msn.com");

        IList<IWebElement> terms = AzimaHome.FindElements(By.TagName("a"));
        terms.First(element => element.Text == "msn").Click();
    }
}

我认为该错误主要是因为您使用的 Microsoft WebDriver 版本与您使用的 Microsoft Edge 版本不匹配。

如果您使用的是 18 之前的Microsoft Edge 版本,则可以在此链接中为您安装的 Microsoft Edge 版本下载相应的 Web 驱动程序。如果您的 Microsoft Edge 版本为18 或更高,您可以按照本文中的方法安装相应的 webdriver 版本。

于 2019-12-24T07:52:58.197 回答