VisibilityOfElementLocated Vs presenceOfElementLocated
You can use both presenceOfElementLocated
or visibilityOfElementLocated
to get the value
.
But for the performance perspective, I would guess that presenceOfElementLocated
will be slightly faster because it's just check that an element is present on the DOM of a page. This does not necessarily mean that the element is visible. while the visibilityOfElementLocated
has to check 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.
So according to your case use of presenceOfElementLocated
will be enough.
you can consider the following point to choose appropriate method depending on your use case.
use
presenceOfElementLocated
when you don't care whether if element visible or not, you just need to know if it's on the page.use
visibilityOfElementLocated
when you need to find element which should be also visible.
Hope it will help you..:)
If you only want to get the value presenceOfElementLocated
is enough to extract the value.
visibilityOfElementLocated
is for testing purposes. To see what happens to an element when you interact with it somehow for example.
presenceOfElementLocated()
presenceOfElementLocated()
is the expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.
public static ExpectedCondition<WebElement> presenceOfElementLocated​(By locator)
Parameters:
locator - used to find the element
Returns:
the WebElement once it is located
visibilityOfElementLocated()
visibilityOfElementLocated()
is the 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.
public static ExpectedCondition<WebElement> visibilityOfElementLocated​(By locator)
Parameters:
locator - used to find the element
Returns:
the WebElement once it is located and visible
This usecase
To get the value of the innerHTML
using Selenium ideally the element needs to be visible instead of just being present. So you have to use visibilityOfElementLocated()
.
Your effective java based code block will be:
Using
visibilityOfElementLocated()
:WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("elementCssSelector"))); System.out.println(element.getAttribute("innerHTML"));
In a single line:
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("elementCssSelector"))).getAttribute("innerHTML"));
Your effective python based code block will be:
Using
visibility_of_element_located()
:element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "element_css_selector"))) print(element.get_attribute("innerHTML"))
In a single line:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "element_css_selector")))).get_attribute("innerHTML"))
Your effective c# based code block will be:
Using
ElementIsVisible()
:var element = new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(By.CssSelector("ElementCssSelector"))); Console.WriteLine(element.GetAttribute("innerHTML"));
In a single line:
Console.WriteLine(new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(By.CssSelector("ElementCssSelector"))).GetAttribute("innerHTML"));