'Selenium C#: How to identify visible or invisible (style="...; display:none") which may be scrolled out of visible area
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
Solution 1:[1]
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.
Solution 2:[2]
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)
Solution 3:[3]
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")));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Prophet |
| Solution 2 | shatulsky |
| Solution 3 | undetected Selenium |
