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

查看:49
本文介绍了为什么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");
    }
}

单击成功后,一切正常,但是当我在try子句中获得WebDriverException时,即使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 hack应用于任何地方.

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)Browser busy属性是真的.将我引向正确的方向.

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")入侵.

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天全站免登陆