【问题标题】:Selenium Chrome Webdriver not workingSelenium Chrome Webdriver 不工作
【发布时间】:2017-02-22 13:58:22
【问题描述】:

我正在尝试使用 Selenium 的 Chrome Webdriver 和 C# 自动登录网站。运行以下代码会引发屏幕截图一和二中列出的错误。我究竟做错了什么?谷歌搜索错误似乎没有显示任何相关结果。

using System.Drawing.Imaging;
using System.IO;
using OpenQA.Selenium.Chrome;

namespace WebDriverTest
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize the Chrome Driver
            using (var driver = new ChromeDriver())
            {
                // Go to the home page
                driver.Navigate().GoToUrl("https://twitter.com/");                    
                // Get User Name field, Password field and Login Button
                var userNameField = driver.FindElementById("usr");
            }
        }
    }
}

    An unhandled exception of type 'OpenQA.Selenium.WebDriverException' occurred in WebDriver.dll. 
    Additional information: A exception with a null response was thrown 
sending an HTTP request to the remote WebDriver server for 
URL http://localhost:61724/session/cc7bae393b288855ed8169dade774baa/element. 
The status of the exception was ReceiveFailure, and the message was: 
The underlying connection was closed: An unexpected error occurred on a receive.

【问题讨论】:

  • 我相信这是版本不匹配。您使用的是什么版本的 chrome 和 chrome 驱动程序?我建议尝试使用最新的 chrome 56 和 chrome driver 2.28。看起来 57 chrome 可能有问题。

标签: c# .net selenium selenium-chromedriver


【解决方案1】:

您必须在GoToUrl 之后等待页面加载:

driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(10));

【讨论】:

  • 得到一个不同的错误但类似的结果:WebDriver.dll 中发生了“OpenQA.Selenium.WebDriverException”类型的未处理异常附加信息:意外错误。 System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: 无法建立连接,因为目标机器主动拒绝了它::1:62419
【解决方案2】:

我会等待页面加载。

public static void WaitForElementLoad(By by, int timeoutInSeconds)
{
    if (timeoutInSeconds > 0)
    {
        WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(timeoutInSeconds));
        wait.Until(ExpectedConditions.ElementIsVisible(by));
    }
}

curtosey How to wait for element to load in selenium webdriver?

【讨论】:

    【解决方案3】:

    验证您的 Chrome 和 ChromeDriver 版本。请参阅兼容性here。还要检查您的 Selenium 版本是否过时,尽管这些类型的错误通常是由于版本兼容性造成的。

    供参考,当前最新消息:

    Chrome 56.0...
    ChromeDriver 2.27
    Selenium WebDriver 3.1.0
    

    【讨论】:

      【解决方案4】:

      很明显!

      页面“http://twitter.com”上没有 ID 为“usr”的元素。

      你在寻找什么? 您正在页面上查找 id 属性设置为“usr”的元素,并且没有这样的 elememt。异常说明了

      请做一个检查元素

      【讨论】:

        猜你喜欢
        • 2018-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多