2
votes

I am using the selenium webdriver to run my automation tests. One of my tests opens a login page, enters credentials and clicks the login button. When I run it locally I can see that it has successfully logged in to the site but then my test throws an error on the click method even though I've seen it move onto the next page. I'm also using Chrome Driver.

The text on the error says:

OpenQA.Selenium.WebDriverException: 'The HTTP request to the remote WebDriver server for URL http://localhost:4444/click timed out after 60 seconds.

Inner Exception WebException: The request was aborted: The operation has timed out.

This is the code I'm using to locate the element and to click it:

private IWebElement LoginButton => Wait.Until(d => Driver.FindElementByCssSelector(".btn"));

private void ClickLoginButton()
{                               
    LoginButton.Click();
}

Other click methods for other sites are working fine, I only seem to be seeing this error with this button.

2
What are your chromedriver and browser versions? - Guy
@guy Chrome browser version is 68.0.3440.106 and chromedriver is 2.41 - Benathan
The browser has navigated to the next page but if you look carefully, the page is still loading in the browser. There are many, many potential reasons for this... slow internet, problems with the page or web server, and so on. Does it consistently happen? Does it happen when you execute the scenario manually? Do you have access to the devs? If so, ask them why it might be happening and maybe they can investigate and find a fix. - JeffC

2 Answers

2
votes

When Selenium executes click() it will try to wait for the page to load. From the docs

the Click() method will attempt to block until the page has loaded

If the IWebDriver doesn't receive this indication (document.readyState if memory serves) it will end with the WebDriverException.

There isn't any "pretty" solution. One option is to set ChromeOption in the driver initialization

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddAdditionalCapability("pageLoadStrategy","none");
IWebDriver driver = new ChromeDriver(chromeOptions);

But this will effect all the tests.

Another option is to catch WebDriverTimeoutException in this particular button

private void ClickLoginButton()
{
    try
    {
        driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(1));    
        LoginButton.Click();
    }
    catch (WebDriverTimeoutException) { }
    finally
    {
        driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(originalTime));
    }
}
-1
votes

I had the same issue: The HTTP request to the remote WebDriver server for URL http://localhost:17553/session/1a94ba744cb6dc03af7d65bf58003eff/element/0.42183143444524296-3/click timed out after 60 second

specifying timeout in ChromeDriver constructor helped in my scenario

            /// <summary>
    /// Initializes a new instance of the <see cref="ChromeDriver"/> class using the specified path
    /// to the directory containing ChromeDriver.exe, options, and command timeout.
    /// </summary>
    /// <param name="chromeDriverDirectory">The full path to the directory containing ChromeDriver.exe.</param>
    /// <param name="options">The <see cref="ChromeOptions"/> to be used with the Chrome driver.</param>
    /// <param name="commandTimeout">The maximum amount of time to wait for each command.</param>
    public ChromeDriver(string chromeDriverDirectory, ChromeOptions options, TimeSpan commandTimeout)
        : this(ChromeDriverService.CreateDefaultService(chromeDriverDirectory), options, commandTimeout)
    {
    }