DoWebDriver findElements preserve row order in table when retrieving it

When getting a list of html table WebElement , like the following (for example, by tag):

webDriver.findElement(By.id("mainTable"))
         .findElements(By.tag("tr"))

      

Is there a guaranteed order for the returned list? Is it safe to assume that [i] comes before [i + 1] in table row order?

I have looked through the official docs and searched google but no luck.

+3


source to share


2 answers


I don't think I've seen it documented anywhere, but in my experience: - findElement returns the first element that matches the locator definition (first = as in DOM) - findElements returns all elements that match the locator definition (DOM ordering)



0


source


Yes, there is a guaranteed order for the returned list, and you can safely assume that item [i] comes before item [i + 1] in table row order.

You can reference the implementation of findElements () method in RemoteWebDriver and By with different parameters. You need the code below for the findElements method, which takes two strings as parameters.

protected List<WebElement> findElements(String by, String using) {
  if (using == null) {
    throw new IllegalArgumentException("Cannot find elements when the selector is null.");
  }
  Response response = execute(DriverCommand.FIND_ELEMENTS,
      ImmutableMap.of("using", by, "value", using));
  Object value = response.getValue();
  List<WebElement> allElements;
  try {
    allElements = (List<WebElement>) value;
  } catch (ClassCastException ex) {
    throw new WebDriverException("Returned value cannot be converted to List<WebElement>: " + value, ex);
  }
  for (WebElement element : allElements) {
    setFoundBy(this, element, by, using);
  }
return allElements;
} 

      



Note that the method returns an ImmutableMap to return the found web items and add them to the list.

A persistent hashmap oriented with reliable custom iteration order.

Since ImmutableMap preserves iteration order, you can safely assume that the return of the WebElement list by findElements is ordered.

0


source







All Articles