Home > Enterprise >  Selenium C#: How to identify visible or invisible (style="...; display:none") which may be
Selenium C#: How to identify visible or invisible (style="...; display:none") which may be

Time:03-17

we have automated a test, where table columns have been set to hidden (style="...; display:none").

Currently we are using IWebElement.Displayed.

This works fine if the whole table is visible (no scrollbar) but if some columns are scrolled out this don't work. Is there a way to check it without checking the style attribute and without scrolling?

If I would need to use scrolling: how can I check for really invisible columns.

Thank you

CodePudding user response:

You can get the element attribute value with GetAttribute() method.

var style = IWebElement.GetAttribute("style");

Then you will be able to check if style string contains display:none etc.

CodePudding user response:

Get element location -

var e = your web element
var elementPosition = new Rectangle(e.Location, e.Size);

Get current screen position

var clientAreaRectangle = new Rectangle(GetScrollOffset(driver), driver.Manage().Window.Size)

public static Point GetScrollOffset(this IWebDriver driver)
{
    var executor = (IJavaScriptExecutor)driver;
    var offset = (Dictionary<string, object>)executor
        .ExecuteScript("return { X: Math.floor(window.pageXOffset), Y: Math.floor(window.pageYOffset) };");
    var offsetX = (int)(long)offset["X"];
    var offsetY = (int)(long)offset["Y"];
    return new(offsetX, offsetY);
}

Check if the element is scrolled into view

clientAreaRectangle.Contains(elementPosition)

CodePudding user response:

Irespective of the desired elements being visible / invisible i.e. with style="display: none;", you can select all the table columns using the following locator strategy:

IList<IWebElement> Columns = driver.FindElements(By.XPath("XPathColumnElements"));

You can also induce WebDriverWait for the PresenceOfAllElementsLocatedBy() as follows:

IList<IWebElement> Columns = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.PresenceOfAllElementsLocatedBy(By.CssSelector("CssSelectorColumnElements")));
  • Related