C# 为什么可以';t Selenium WebDriver在catch子句中查找my元素

C# 为什么可以';t Selenium WebDriver在catch子句中查找my元素,c#,internet-explorer,selenium,selenium-webdriver,webdriver,C#,Internet Explorer,Selenium,Selenium Webdriver,Webdriver,我正在尝试开发一种用Internet Explorer点击东西的黑客。我的目标是使用一种方法,首先尝试正常的Click(),如果失败,将执行SendKeys(“\n”),这似乎是公认的解决方法 这是我的尝试 public void ClickByCssSelectorIeSafe(string cssSelector) { try { _driver.FindElement(By.CssSelector(cssSelector)).Click(); }

我正在尝试开发一种用Internet Explorer点击东西的黑客。我的目标是使用一种方法,首先尝试正常的
Click()
,如果失败,将执行
SendKeys(“\n”)
,这似乎是公认的解决方法

这是我的尝试

public void ClickByCssSelectorIeSafe(string cssSelector)
{
    try
    {
        _driver.FindElement(By.CssSelector(cssSelector)).Click();
    }
    catch (WebDriverException)
    {
        _driver.FindElement(By.CssSelector(cssSelector)).SendKeys("\n");
    }
}
当单击成功时,一切正常,但当我在try子句中获得WebDriverException时,catch子句中的FindElement失败,即使它在try子句中成功。为什么?

另一个有趣的地方是,在某些情况下,我可以看到
Click()
在浏览器中成功,但它仍然抛出异常,并最终出现在catch子句中

我之所以要这样做,是因为我们正在Chrome、Firefox和IE上运行测试,我不希望IE黑客应用于任何地方

catch子句中失败的FindElement的异常消息如下所示

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.
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.
try子句中单击失败的异常消息如下所示

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.
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.

尝试将您的代码更改为以下代码,以便找出问题

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");
    }
}
现在,您将知道在查找元素或单击元素时是否引发异常,并且仍然能够处理这两种情况


但是,在这两种情况下,您似乎都遇到超时问题,这表明浏览器/AUT挂起/没有响应。查看selenium服务器和节点日志了解更多信息,以了解引发异常之前发生的情况。

我最终在日志中找到了这一点:
D 2015-04-27 14:01:08:497 Browser.cpp(379)Browser busy属性为true。
这将我引向了正确的方向

我面临的问题似乎是页面很忙,不允许我与之交互。我找到了一个建议 页面加载超时并在发生异常时处理(吞咽)异常。成功了

换言之,如果页面忙,我只接受异常,如果由于其他原因单击失败,我将执行
SendKeys(“\n”)
hack

因此,当我初始化驱动程序时,我会:

driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(5));
现在,我的扩展方法如下所示:

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

感谢@user903039帮助我找到问题。

我想您会发现引发异常的原因实际上是找不到元素,而不是单击失败。考虑到这一点,它也无法在catch块中找到元素是完全有道理的。@Steve,如果我只是立即运行catch块中的内容,它就会工作。谢谢,我将元素初始化为null以使代码编译。这给了我一个稍微不同的错误消息,但它仍然来自
元素。SendKeys(“\n”)
。新消息是
对远程WebDriver服务器的URL的HTTP请求http://localhost:63046/session/9a7df226-3f65-491d-a874-75b64dd83068/element/e4cff498-22ab-494f-b20f-384d961baf27/值在60秒后超时。
另一件有趣的事情是,我可以在浏览器中看到单击成功。@JohanGov它看起来像是新的异常来自测试代码的下一行。正如您所说,单击成功,因此正在执行测试中的下一行/e4cf‌​f498-22ab-494f-b20f-384d961baf27/异常消息中的值表示它正在尝试获取元素的值,可能是文本框?
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");
    }
}