使用硒浏览分页

时间:2019-11-07 16:22:43

标签: selenium-webdriver

https://www.seleniumeasy.com/test/table-pagination-demo.html

正在尝试自动执行以上页面。当我从第一页获取所有名称时使用CSS使其从所有3页获取所有名称。这从所有的名称而不是第一页的CSS。 By.cssSelector(“。table.table-hover> tbody> tr> td:nth-​​child(2)”);

我尝试了以下代码

int paginationsize = tablepagination.PaginationForLoop().size();

    System.out.println(paginationsize);

    ArrayList<String> names = new ArrayList<String>();

    for (int i = 2; i < paginationsize; i++) {

        // Get common xpath/css for the page buttons and loop through them one by one
        String paginationSelector = "/html[1]/body[1]/div[2]/div[1]/div[2]/div[1]/ul[1]/li[" + i + "]/a[1]";
        driver.findElement(By.xpath(paginationSelector)).click();

        System.out.println("Selected the page " + i);
        Thread.sleep(2000);

        // Get the names of all the elements for page i
    //  java.util.List<WebElement> namesElements = tablepagination.FirstColumnNumbers();

         java.util.List<WebElement> namesElements = driver.findElements(By.cssSelector(".table.table-hover>tbody>tr>td:nth-child(2)"));

        // loops through all the names and add those name to the defined names list
        for (WebElement namesElements1 : namesElements) {

            names.add(namesElements1.getText());
        }

        //loop through the names and print all the names
        //Issue this is printing all the names from 3 pages. I just need to print 1st page then 2nd and then 3rd.
        for (String testname : names) {
            System.out.println(names);
        }

    }

    int totalNames = names.size();

    //Instead of printing 13 it prints 39 as total names
    System.out.println("Total Number of Names: " + totalNames);

我如何才能仅从第一页获得5个名称,然后从第二页获得5个名称,然后从第三页获得3个名称。从我的css seletor,它从第1页获得所有13个名称,而不仅仅是5个名称。需要帮助。

0 个答案:

没有答案