Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium WebDriver - Assert element not present on screen

Afternoon,

I've searched but cannot find an answer to an issue I'm having with selenium. I need to find a way to ensure elements are not displayed on a page. Basically, our users have varying levels of permissions, which results in different tabs being displayed.

We've had a few issues recently and incorrect tabs have been displayed for users that shouldn't see them. Now asserting an element is present is no issue at all, but this wont ensure rouge tabs are not present. Any help would be appreciated on this one, as it's proving difficult and I'm sure this isn't that complicated.

We're using Selenium WebDriver and writing the tests in C#.

Thanks,

Stu.

like image 402
StuChee Avatar asked Mar 27 '26 21:03

StuChee


2 Answers

Use webdriver's findElements method, if the List of WebElements returns size as 0, you can assert for the count of elements.

like image 124
Gokul Avatar answered Mar 29 '26 12:03

Gokul


To ensure rouge tabs are not present you can use either of the ExpectedConditions clause from the following :

  • ExpectedConditions.InvisibilityOfElementLocated Method :

ExpectedConditions.InvisibilityOfElementLocated can be used for checking that an element is either invisible or not present on the DOM.

Return Value : true if the element is not displayed, otherwise false.

  • ExpectedConditions.InvisibilityOfElementWithText Method :

ExpectedConditions.InvisibilityOfElementWithText can be used for checking that an element with text is either invisible or not present on the DOM.

Return Value : true if the element is not displayed, otherwise false.

like image 41
undetected Selenium Avatar answered Mar 29 '26 12:03

undetected Selenium