0

正如标题所说。我有一段代码,其中这种技术是 IMO 合理的。

我有一个带有table的页面,每一行都包含产品名称,其中 id 取决于行号:

<table>
  <tr><td>...</td><td name ="product1">i fon</td></tr>
  <tr><td name ="product2">TV</td></tr>
</table>

我正在查找带有 PageFactory 注释的行:

@FindBy(How= blablabla)
List<WebElement> rows;

只有这样我才知道页面上有多少产品,所以我要找到每个产品:

    List<String> productNames = new Vector<>();
Iterator<WebElement> itemRowsIter = rows.iterator();

    int rowIndex = 0;
    while (itemRowsIter.hasNext()) {
        By itemLocator = By.name("product" + rowIndex);
        WebElement link = quietlyFindElement(driver, itemLocator, itemRowsIter.next());
        Optional<String> text = Optional.ofNullable(link)
                .map(WebElement::getText);
        text.ifPresent(productNames::add);
        rowIndex++;
    }

quietFindElement 在 Selenium 中搜索元素,捕获 NoSuchElementException 和 TimeoutException,如果它们被抛出则返回 null。

这种溶液干净吗?

4

0 回答 0