Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an element is visible with WebDriver

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?

like image 637
ponzao Avatar asked Apr 15 '10 14:04

ponzao


People also ask

How do you check if an element is visible?

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.

How do I make an element visible in Selenium Webdriver?

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.

Which method is used to check if an element is visible on the web page?

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.


1 Answers

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.

like image 63
sleske Avatar answered Sep 28 '22 21:09

sleske