使用Selenium浏览分页,将项目添加到HashMap [英] Navigating through pagination with Selenium adding items to a HashMap

查看:169
本文介绍了使用Selenium浏览分页,将项目添加到HashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

网页上的分页看起来像这样:1、2、3、4、5,下一步.这些都是链接,单击下一步"时,它将导航到第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;
    }
}

I have a list of WebElements on a results page that I need to extract and add to a HashMap.

There is pagination on the webpage that looks like this: 1, 2, 3, 4, 5, Next. These are all links and when Next is clicked on, it will then navigate to Page 2 and display 6 on the pagination, whilst removing 1, and so on.

    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);
        }

The getAuthors() method goes through the necessary elements on the page and adds them to the HashMap. So it will loop through all of the pages in the pagination list until it is completed. It will go back to the Page 1 which is saved as Constants.url.

It gets to page 5 but then gets stuck, I am not sure how to code in the other examples of 6, 7, 8 and clicking the Next button each time to access them, adding them to pagination list.

Note: The Thread.sleep methods are there to enable the page time to load all elements on the page.

Any ideas?

解决方案

You can do something like this, assuming that the link is an <a> html element you need

1 Create a List of Web elements for example <a> elements driver.findElements(By.tagName("a"))

2 Iterate List retrieved from the last step and check some meta information of the specific link that you need to click on, for example the title elements.get(i).getAttribute("title");

3 Compare if the title is the one you need if (title.equals("Next Page")) {

3 Send click signal

Here is a code snippet

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;
    }
}

这篇关于使用Selenium浏览分页,将项目添加到HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆