为什么Selenium WebDriver不能在catch子句中找到我的元素 [英] Why can't Selenium WebDriver find my element in a catch clause

查看:142
本文介绍了为什么Selenium WebDriver不能在catch子句中找到我的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Internet Explorer开发一个用于点击内容的黑客攻击。我的目标是有一个我可以使用的方法,首先尝试正常的 Click(),如果失败将执行 SendKeys(\\ \\ n)这似乎是公认的解决方法。

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.

这是我的尝试

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

当点击成功时一切正常但当我得到WebDriverException时在try子句中,即使在try子句中成功,catch子句中的FindElement也会失败。为什么?

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?

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

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.

我想要这个,因为我们在Chrome,Firefox和IE中运行我们的测试,我不想要IE浏览器无处不在。

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

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

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.

try子句中失败点击的异常消息如下所示

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.


推荐答案

我最终在日志中找到了这个: D 2015-04-27 14:01:08:497 Browser.cpp(379)浏览器繁忙属性为真。这使我朝着正确的方向前进。

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.

换句话说,如果页面繁忙,我只是吞下异常,如果点击因其他原因失败,我会执行 SendKeys(\\ \\ n) hack。

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

感谢@ user903039帮助我发现了问题。

Thanks to @user903039 for helping me find the problem.

这篇关于为什么Selenium WebDriver不能在catch子句中找到我的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆