With WebDriver
from Selenium 2.0a2 I am having trouble checking if an element is visible.
WebDriver.findElement
returns a WebElement
, which unfortunately doesn't offer an isVisible
method. I can go around this by using WebElement.clear
or WebElement.click
both of which throw an ElementNotVisibleException
, but this feels very dirty.
Any better ideas?
We can also confirm if an element is visible with the help of isDisplayed() method. This method returns a true or a false value. In case the element is invisible, the method returns a false value.
We can create a JavaScript Executor for making an element visible in Selenium webdriver. A hidden element has a style attribute whose value set to display: none. JavaScript Executor can make the same element visible on the page. Selenium executes JavaScript commands with the help of the executeScript method.
isDisplayed() is the method used to verify a presence of a web element within the webpage. The method returns a “true” value if the specified web element is present on the web page and a “false” value if the web element is not present on the web page.
Even though I'm somewhat late answering the question:
You can now use WebElement.isDisplayed()
to check if an element is visible.
Note:
There are many reasons why an element could be invisible. Selenium tries cover most of them, but there are edge cases where it does not work as expected.
For example, isDisplayed()
does return false
if an element has display: none
or opacity: 0
, but at least in my test, it does not reliably detect if an element is covered by another due to CSS positioning.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With