11

Consider this:

val element = ...
String str = element.getAttribute("innerHTML")

So in case i only wants to get this value is it enough to use presenceOfElementLocated() instead of visibilityOfElementLocated() ?

4

3 回答 3

34

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.

Hope it will help you..:)

于 2016-06-27T05:51:14.457 回答
1

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.

于 2016-06-26T13:46:23.517 回答
1

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 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 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 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"));
    
于 2021-01-13T11:56:54.773 回答