0

I am trying to automate logging into a website using Selenium's Chrome Webdriver and C#. Running the following code throws the error listed in screenshot one and two. What am I doing wrong? Googling the error does not seem to show any relevant results.

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

enter image description here

    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.

enter image description here

Ajit Goel
  • 4,180
  • 7
  • 59
  • 107
  • I believe this is a version mismatch. What is the version of chrome and chrome driver you are using? I would suggest trying with chrome 56 and chrome driver 2.28 which is latest. Looks like 57 chrome might have some issue. – Shishir Kanthi Mar 26 '17 at 14:51

4 Answers4

0

You have to wait the page loading just after GoToUrl :

driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(10));
A.Baudouin
  • 2,855
  • 3
  • 24
  • 28
  • Getting a different error but similar results: An unhandled exception of type 'OpenQA.Selenium.WebDriverException' occurred in WebDriver.dll Additional information: Unexpected error. System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it ::1:62419 – Ajit Goel Feb 22 '17 at 14:16
0

I would wait for the page to load.

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?

Community
  • 1
  • 1
CBRRacer
  • 4,649
  • 1
  • 23
  • 27
0

Verify your versions of Chrome and ChromeDriver. See compatibility here. Also check your Selenium version isn't out of date, though these types of errors are usually because of version compatibility.

For reference, current latests:

Chrome 56.0...
ChromeDriver 2.27
Selenium WebDriver 3.1.0
Moshisho
  • 2,781
  • 1
  • 23
  • 39
0

It is clear!

There is no element on page "http://twitter.com" having id "usr".

What are you searching for? You are looking for an element on page which has id attribute set to "usr" and there is no such elememt. The exception says it

Do an inspect element plz

Efe
  • 800
  • 10
  • 32