How to check if an item is visible using WebDriver

With Selenium 2.0a2's WebDriver, I am having trouble checking if an item is visible.

WebDriver.findElement returns a WebElement , which, unfortunately, does not offer the isVisible method. I can get around this using WebElement.clear or WebElement.click , both of which ElementNotVisibleException an ElementNotVisibleException , but it is very dirty.

Any better ideas?

+52
java selenium webdriver
Apr 15 2018-10-15T00:
source share
10 answers

element instanceof RenderedWebElement should work.

+16
Apr 15 2018-10-15T00:
source share

Although I was a bit late in answering the question:

Now you can use WebElement.isDisplayed() to check if the item is visible.

+117
Sep 23 2018-11-11T00:
source share

I have the following two suggested methods:

  • You can use isDisplayed() as shown below:

     driver.findElement(By.id("idOfElement")).isDisplayed(); 
  • You can define a method as shown below and call it:

     public boolean isElementPresent(By by) { try { driver.findElement(by); return true; } catch (org.openqa.selenium.NoSuchElementException e) { return false; } } 

Now you can make a statement as shown below to check if this element is present or not:

 assertTrue(isElementPresent(By.id("idOfElement"))); 
+12
Oct 11
source share

If you are using C #, this will be driver.Displayed. Here is an example from my own project:

 if (!driver.FindElement(By.Name("newtagfield")).Displayed) //if the tag options is not displayed driver.FindElement(By.Id("expand-folder-tags")).Click(); //make sure the folder and tags options are visible 
+7
Nov 27
source share

A confirmation element is visible.

 public static boolean isElementVisible(final By by) throws InterruptedException { boolean value = false; if (driver.findElements(by).size() > 0) { value = true; } return value; } 
+2
Jun 12 '14 at 2:06
source share

Here's how I do it (please ignore the calls to the Logger class):

 public boolean isElementExist(By by) { int count = driver.findElements(by).size(); if (count>=1) { Logger.LogMessage("isElementExist: " + by + " | Count: " + count, Priority.Medium); return true; } else { Logger.LogMessage("isElementExist: " + by + " | Could not find element", Priority.High); return false; } } public boolean isElementNotExist(By by) { int count = driver.findElements(by).size(); if (count==0) { Logger.LogMessage("ElementDoesNotExist: " + by, Priority.Medium); return true; } else { Logger.LogMessage("ElementDoesExist: " + by, Priority.High); return false; } } public boolean isElementVisible(By by) { try { if (driver.findElement(by).isDisplayed()) { Logger.LogMessage("Element is Displayed: " + by, Priority.Medium); return true; } } catch(Exception e) { Logger.LogMessage("Element is Not Displayed: " + by, Priority.High); return false; } return false; } 
+1
May 22 '15 at 11:02
source share

It is important to see if the element is visible or not, because Driver.FindElement will only check the HTML source. But the pop-up code may be on the html page and not be visible. Therefore, the Driver.FindElement function returns a false positive result (and your test will fail)

0
Aug 18 2018-11-18T00:
source share
 public boolean isElementFound( String text) { try{ WebElement webElement = appiumDriver.findElement(By.xpath(text)); System.out.println("isElementFound : true :"+text + "true"); }catch(NoSuchElementException e){ System.out.println("isElementFound : false :"+text); return false; } return true; } text is the xpath which you would be passing when calling the function. the return value will be true if the element is present else false if element is not pressent 
0
Feb 14 '17 at 10:55
source share

element instanceof RenderedWebElement should work .// But this is for an older version of selenium rc.

Note:

RenderedWebElement was deprecated four years ago (in 2013). It was maintained until selenium-2.0-rc-2 and is being removed from selenium-2.0-rc-3 forward

Thus, in the latest version there is no such class RenderedWebElement . The current version is 2.46.0. Try using the latest version.

Please use WebElement Instead you do not have to drop everything with isDisplayed() isEnabled() and driver.findElements(By.xpath(accessor)).size() > 0

Something like that:

 public static boolean isElementFoundDisplayedEnabled(WebDriver driver, String accessor){ return driver.findElements(By.xpath(accessor)).size() > 0 && driver.findElement(By.xpath(accessor)).isDisplayed() && driver.findElement(By.xpath(accessor)).isEnabled(); //isDisplayed(): method avoids the problem of having to parse an element "style" attribute to check hidden/visible. False when element is not present //isEnabled(): generally return true for everything but disabled input elements. } 
0
Aug 30 '17 at 14:06 on
source share

try it

 public boolean isPrebuiltTestButtonVisible() { try { if (preBuiltTestButton.isEnabled()) { return true; } else { return false; } } catch (Exception e) { e.printStackTrace(); return false; } } 
-one
Oct 18 '14 at 10:26
source share



All Articles