OpenQA.Selenium.WebDriverException

0

I'm starting to do small automated tests with Selenium webdriver and Visual Studio. Generated a unit test project with the following code:

Then I downloaded and installed the corresponding webdriver. When running the tests it opens the browser instance and generates this exception:

  

Result Message:
  The UnitTestProject2.UnitTest1.TestChromeDriver test method produced the exception:   OpenQA.Selenium.WebDriverException: Unexpected error. System.Net.WebException: It is not possible to connect to the remote server --- > System.Net.Sockets.SocketException: Unable to establish a connection since the destination computer expressly denied such connection 127.0.0.1:46491      in System.Net.Sockets.Socket.DoConnect (EndPoint endPointSnapshot, SocketAddress socketAddress)      in System.Net.ServicePoint.ConnectSocketInternal (Boolean connectFailure, Socket s4, Socket s6, Socket & socket, IPAddress & address, ConnectSocketState state, IAsyncResult asyncResult, Exception & exception)      --- End of stack tracking of internal exception ---      in System.Net.HttpWebRequest.GetRequestStream (TransportContext & context)      in System.Net.HttpWebRequest.GetRequestStream ()      in OpenQA.Selenium.Remote.HttpCommandExecutor.Execute (Command commandToExecute)      in OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute (Command commandToExecute)      in OpenQA.Selenium.Remote.RemoteWebDriver.Execute (String driverCommandToExecute, Dictionary 2 parameters) Seguimiento de la pila de Result:
en OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse) en OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary
2 parameters)      in OpenQA.Selenium.Remote.RemoteWebDriver.FindElement (String mechanism, String value)      in OpenQA.Selenium.Remote.RemoteWebDriver.FindElementById (String id)      in OpenQA.Selenium.By. < > c__DisplayClass2.b__0 (ISearchContext context)      in OpenQA.Selenium.By.FindElement (ISearchContext context)      in OpenQA.Selenium.Remote.RemoteWebDriver.FindElement (By by)

I would like to know how to solve this error. Thanks.

    
asked by Gonzalo Iribe Cisneros 09.07.2017 в 03:46
source

1 answer

0

My Selenium worked perfectly for me from minute 1. Yours seems like a connection problem with the port. The problem could be that the port is occupied by another process and therefore driver can not access it. Or it could also be that you had some kind of firewall or antivirus blocking that connection. Try a netstat -a -n to see if the port is busy.

Anyway, that kind of code is going to give you problems if you do not add code that waits for the page to be loaded. Since Selenium does not do it automatically, searching for an item on a page that has not finished loading will give you an exception. For that I recommend the following article: link

If you are interested, this is how I would do it:

[TestClass]
public class UnitTest1
{
    IWebDriver driverGc;

    [TestInitialize]
    public void SetUp()
    {
        driverGc = new ChromeDriver();
    }

    private void Wait(string id, int seconds)
    {
        WebDriverWait wait = new WebDriverWait(driverGc, TimeSpan.FromSeconds(seconds));

        Func<IWebDriver, bool> waitForElement = new Func<IWebDriver, bool>((IWebDriver Web) =>
        {
            Web.FindElement(By.Id(id));
            return true;
        });
        wait.Until(waitForElement);
    }

    [TestMethod]
    public void TestMethod1()
    {
        driverGc.Navigate().GoToUrl("http://www.google.com");
        Wait("lst-ib", 10);
        driverGc.FindElement(By.Id("lst-ib")).SendKeys("Selenium");
        driverGc.FindElement(By.Id("lst-ib")).SendKeys(Keys.Enter);
    }
}
    
answered by 09.07.2017 в 15:45