Why can't Selenium WebDriver find my element i

2019-08-10 01:11发布

问题:

I'm trying to develop a hack for clicking things with Internet Explorer. My goal is to have one method that I can use that will first try a normal Click() and if it fails will do a SendKeys("\n") which seems to be the accepted workaround.

This is my attempt

public void ClickByCssSelectorIeSafe(string cssSelector)
{
    try
    {
        _driver.FindElement(By.CssSelector(cssSelector)).Click();
    }
    catch (WebDriverException)
    {
        _driver.FindElement(By.CssSelector(cssSelector)).SendKeys("\n");
    }
}

When click succeeds everything works but when I get a WebDriverException in the try clause the FindElement in the catch clause fails even though it succeeded in the try clause. Why?

Another interesting point is that in some cases I can see the Click() succeed in the browser but it still throws the exception and ends up in the catch clause.

I want this because we're running our tests in Chrome, Firefox and IE and I don't want the IE hack applied everywhere.

The exception message for the failing FindElement in the catch clause looks like this

A first chance exception of type 'OpenQA.Selenium.WebDriverException' occurred in WebDriver.dll

Additional information: The HTTP request to the remote WebDriver server for URL 
http://localhost:58124/session/21337088-7630-4709-a902-0a5d1bc7a669/element timed out after 60 seconds.

The exception message for the failing click in the try clause looks like this

A first chance exception of type 'OpenQA.Selenium.WebDriverException' 
occurred in WebDriver.dll

Additional information: The HTTP request to the remote WebDriver server for URL 
http://localhost:58124/session/21337088-7630-4709-a902-0a5d1bc7a669/element/bcee1534-00e6-4155-b4cc-7171db39f112/click timed out after 60 seconds.

回答1:

Try changing your code to the below in order to single out the problem.

public void ClickByCssSelectorIeSafe(string cssSelector)
{
    IWebElement element = null;
    try
    {
        element = _driver.FindElement(By.CssSelector(cssSelector));
        element.Click();
    }
    catch (NoSuchElementException e)
    {
        Console.WriteLine("element not found. {0}", e.Message);
        //do something here when your element is not found
    }
    catch (WebDriverException e)
    {
        if (element != null) element.SendKeys("\n");
    }
}

Now you'll know if the exception is thrown when finding the element or when clicking it and still be able to handle both cases.

However, it looks like you are getting timeout issues in both cases which suggests the browser/AUT is hung/not responding. Check the selenium server and node logs for more information to find out what happened before the exception is thrown.



回答2:

I eventually found this in the log: D 2015-04-27 14:01:08:497 Browser.cpp(379) Browser busy property is true. which lead me in the right direction.

The problem I'm facing seems to be that the page is busy and isn't letting me interact with it. I found a suggestion here to set the page load timeout and handle (swallow) the exception when that happens. That worked.

In other words if the page is busy I just swallow the exception and if the click failed for some other reason I do the SendKeys("\n") hack.

So when I initialize my driver I do:

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

And my extension method now looks like this:

    public static void ClickWithIeHackFailover(this IWebElement element)
    {
        try
        {
            element.Click();
        }
        catch (WebDriverException e)
        {
            if (e.Message != "Timed out waiting for page to load.")
            {
                element.SendKeys("\n");
            }
        }
    }

Thanks to @user903039 for helping me find the problem.