本文介绍了WebDriver findElements在检索时是否保留Table的行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在检索 html表WebElement列表时,例如以下内容(例如,按标记):

When retrieving an html table WebElement list such as the following (e.g. by tag):

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

返回列表是否有保证的订单?我可以安全地假设element [i]在表行顺序中排在element [i + 1]之前吗?

Is there a guaranteed order for returned list? Can I safely assume element[i] comes before element[i+1] in the table rows order?

我研究了官方文档,然后四处搜寻,但没有运气.

I've looked into the official docs, and googled around, but no luck.

推荐答案

是的,返回列表有保证的顺序,您可以放心地假设element [i]在表行顺序中位于element [i + 1]之前.

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

您可以在 RemoteWebDriver 类使用不同的参数.您需要的是findElements方法的以下代码,该代码需要两个字符串作为参数.

You can refer the implementation of findElements() method in RemoteWebDriver and By class with different parameters. What you need is the below code for findElements method, which takes two string 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;
} 

请注意,该方法使用 ImmutableMap 返回找到的Web元素并将它们添加到列表中.

Notice that the method uses ImmutableMap to return the web elements found and add them to a list.

由于ImmutableMap保留了迭代顺序,因此您可以放心地假定findElements方法返回的WebElement列表是有序的.

Since, ImmutableMap retains the iteration order, you can safely assume the WebElement list return by findElements method is ordered.

这篇关于WebDriver findElements在检索时是否保留Table的行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 23:23