繁体   English   中英

使用Selenium浏览分页,将项目添加到HashMap

[英]Navigating through pagination with Selenium adding items to a HashMap

我在结果页面上有一个WebElement列表,需要将其提取并添加到HashMap中。

网页上的分页如下:1,2,3,4,5,Next。 这些都是链接,单击“下一步”时,它将导航到第2页并在分页上显示6,同时删除1,依此类推。

    List<WebElement> pagination = driver.findElements(By.xpath("//div[@class='pagination-container']//a"));
        int s = pagination.size();
        for(int i=0;i<=s;i++){
            this.getAuthors();
                driver.get(Constants.url);
                Thread.sleep(5000);

            pagination = driver.findElements(By.xpath("//div[@class='pagination-container']//a"));
            pagination.get(i).click();
            Thread.sleep(5000);
        }

getAuthors()方法遍历页面上的必要元素,并将其添加到HashMap中。 因此它将遍历分页列表中的所有页面,直到完成。 它将返回到第1页,该页另存为Constants.url。

它进入第5页,但随后陷入困境,我不确定如何在其他示例6、7、8中进行编码,并每次单击“下一步”按钮来访问它们,并将它们添加到分页列表中。

注意:那里的Thread.sleep方法用于启用页面时间以加载页面上的所有元素。

有任何想法吗?

您可以执行以下操作,假设链接是您需要的<a> html元素

1创建一个Web元素列表,例如<a>元素driver.findElements(By.tagName("a"))

2从最后一步检索的迭代列表,并检查您需要单击的特定链接的一些元信息,例如标题elements.get(i).getAttribute("title");

3比较标题是否是您所需要的标题if (title.equals("Next Page")) {

3发送点击信号

这是一个代码片段

new WebDriverWait(
            driver, TIME_TO_WAIT).until(
                    ExpectedConditions.presenceOfElementLocated(
                            By.tagName("a")));
List<WebElement> elements = driver.findElements(By.tagName("a"));
for (int i = 0; i < elements.size(); i++) {
    String title = elements.get(i).getAttribute("title");
    if (title.equals("Next Page")) {
        elements.get(i).click();
        break;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM