Selenium声称Displayed是真的,当它不可见时(需要向下滚动)

时间:2017-03-13 08:49:39

标签: c# selenium

我有一个包含表格数据的页面。滚动导致所有数据都不适合一页。我想断言最后一行是不可见的。我有一个代码:

var element = _driver.FindElement(By.Id("last-element"));
element.Displayed // !!! true

我知道它可能已被正确解析,未被样式等隐藏,但肯定用户不可见。

我创建了:

static bool IsVisible(IWebElement webElement)
{
    if (webElement.Displayed == false)
    {
        return false;
    }

    RemoteWebElement remote = (RemoteWebElement) webElement;

    return remote.Location.Y == remote.LocationOnScreenOnceScrolledIntoView.Y;
}

但它表现得更加陌生。代码到达LocationOnScreenOnceScrolledIntoView后,浏览器会滚动到该位置。

Selenium 3.3,ChromeDriver 2.28(与FirefoxDriver 0.14相同)

我错过了什么?

1 个答案:

答案 0 :(得分:1)

    public bool Displayed(string elementTagName, string elementAttribute, string value)
    {
            var result = false;
            try
            {
                var elementDisplayed = ((IJavaScriptExecutor)driver).ExecuteScript($"return $(\"{elementTagName}[{elementAttribute}=\'{value}\']:visible\").length").ToString();
                result = string.Compare("0", elementDisplayed, StringComparison.OrdinalIgnoreCase) == 0;
            }
            catch (Exception)
            {
                //ignore exception
            }

            return result;
    }
如果未显示元素,则

返回true;如果显示元素,则返回false。

只需传递元素变量,Displayed("div","class","rawr");

即可
相关问题