0

I have two questions:

1)What is the difference between ExpectedConditions.visibilityOfElementLocated() and ExpectedConditions.presenceOfElementLocated()? I am asking in context of visible and presence?what does here visible and presence means? does visible means visible in maximized browser window?

2)What if I use ExpectedConditions.visibilityOfElementLocated() and minimize the browser? Will it give TimeoutException?

a Learner
  • 4,944
  • 10
  • 53
  • 89

2 Answers2

0

https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#visibilityOf(org.openqa.selenium.WebElement)

visibilityOfElementLocated:

public static ExpectedCondition visibilityOfElementLocated​(By locator) An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0. Parameters: locator - used to find the element Returns: the WebElement once it is located and visible

presenceOfElementLocated:

public static ExpectedCondition presenceOfElementLocated​(By locator) An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible. Parameters: locator - used to find the element Returns: the WebElement once it is located

in visibility it checks for display property and also the rendered rectangle size of the image

In present it just check if the element is present in the DOM,

Example a tooltip text that gets displayed only when we hover over it , do the element is present always but visible only when we hover over it

PDHide
  • 18,113
  • 2
  • 31
  • 46
0

presenceOfElementLocated()

presenceOfElementLocated() is the expectation for checking that an element is present on the DOM of a page. It is defines as:

public static ExpectedCondition<WebElement> presenceOfElementLocated​(By locator)
An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.

Parameters:
locator - used to find the element

Returns:
the WebElement once it is located

So presenceOfElementLocated doesn't confirms the element is visible with in the DOM Tree. This phenomenon can be observed with WebElement having:

  • CSS visibility property set as hidden. As an example:

    h2.b {
      visibility: hidden;
    }   
    
  • style attribute is set as "display: none;". An example:

    <select class="pretty-dropdown" id="alBrandsList" style="display: none;">
    

visibilityOfElementLocated()

visibilityOfElementLocated() is the expectation for checking that an element is present on the DOM of a page and visible. It is defined as:

public static ExpectedCondition<WebElement> visibilityOfElementLocated​(By locator)
An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.

Parameters:
locator - used to find the element

Returns:
the WebElement once it is located and visible

visibilityOfElementLocated() with the browser minimized

Visibility/invisibility of the minimized browser is subjected to our naked eyes where as presenceOfElementLocated / visibilityOfElementLocated is subjective to Selenium.

You can find a relevant detailed discussion in How to execute tests with selenium webdriver while browser is minimized


References

You can find a couple of relevant detailed discussions on element displayedness in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352