页面末尾迭代returns false

Iteration at the end of the end of pages returns false

我在测试中有一些方法可以正常工作,但是当页面结束时它 returns 错误。如何确保方法运行后返回true?

我知道我需要修复 initNextPageButton 方法中的逻辑,但我不明白如何

public List<String> collectResultsFromPage() {
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='_8v6CF']")));
        List<WebElement> resultsNameWebElement = driver.findElements(By.xpath("//article//h3[@data-zone-name = 'title']//span"));
        return resultsNameWebElement.stream()
                .map(WebElement::getText)
                .collect(Collectors.toList());
    }

private boolean initNextPageButton() {
    try {
        goNextPageButton = driver.findElement(By.xpath("//a[contains(@class, '_3OFYT')]"));
        return true;
    } catch (NoSuchElementException e) {
        return false;
    }
}

public void collectResults() {
    findResults = collectResultsFromPage();
    while (initNextPageButton()) {
        goNextPageButton.click();
        findResults.addAll(collectResultsFromPage());
    }
}

我相信有多种方法可以做到这一点,在这里我会使用 findElements(复数)来检查列表大小,如果它包含的元素多于零则继续,return true 否则 return false.

private boolean initNextPageButton() {
        List<WebElement> goNextPageButton = driver.findElements(By.xpath("//a[contains(@class, '_3OFYT')]"));
        if(goNextPageButton.size() > 0){
           goNextPageButton.get(0).click();
           return true;
         }
        else
           return false;
}