使用 selenium java 在多个页面的产品列表中迭代 [英] iteration in product list of several pages with selenium java

查看:22
本文介绍了使用 selenium java 在多个页面的产品列表中迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要查看产品列表:

  • 点击它
  • 在产品页面上执行一些操作(这些操作很简单并且已经完成)
  • 回到我已经在列表中
  • 重复该页面的所有产品

完成当前页面的产品列表后,我希望能够单击下一步并与下一页的产品执行相同的过程.

这就是问题所在:当它达到当前页面的10个项目时,我不知道如何切换到另一个页面并重新开始.

示例代码 html:

<html lang="pt-br"><头><title>产品</title><meta charset="utf-8"><身体><div class="srp-actions blue-button"><a class="primary-action-button label" href="">Produto 1</a></div><div class="srp-actions blue-button"><a class="primary-action-button label" href="">Produto 2</a></div><div class="srp-actions blue-button"><a class="primary-action-button label" href="">Produto 3</a></div><div class="srp-actions blue-button"><a class="primary-action-button label" href="">Produto 4</a></div><br/><div id="分页">
    <li class="active">1</li><li class="link"><a class="page-link" href="" title="Página 2">2</a></li><li class="link"><a class="page-link" href="" title="Página 2">3</a></li><li class="link"><a class="page-link" href="" title="Página 2">4</a></li><li class="next"><a class="page-link" href="" rel="next" title="Avançar">next</a></li>

</html>

Java 代码:

int size = driver.findElements(By.className("page-link")).size();System.out.println("页数:" + size);for(int j = 1 ; j <大小 ; j++) {if (j <2) {//我们不需要导航到第一页driver.findElement(By.linkText("Avançar >")).click();//导航到页码 j}String pagesearch = driver.getCurrentUrl();for(int i=1;i

解决方案

基本逻辑是:

  1. 转到第一个产品列表页面.
  2. 获取所有产品链接.导航到每个产品页面并执行操作.
  3. 返回产品列表页面并点击下一步.

您将重复 2 &3 直到没有下一个链接.

String url = "http://www.example.com";//产品列表页面的第一页driver.get(url);列表下一个;而(真){//等待并获取所有产品链接WebDriverWait wait = new WebDriverWait(driver, 10);列表productLinks = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.linkText("Produto")));//遍历产品链接for (WebElement productLink : productLinks){driver.get(productLink.getAttribute("href"));//导航到产品页面//在产品页面上做一些事情}//现在我们已经完成了这个页面上的所有产品,回到我们上次访问的产品列表页面driver.get(url);//寻找下一个链接next = driver.findElements(By.cssSelector("li.next"));如果 (next.isEmpty()){//下一个链接不存在,退出循环休息;}//下一个链接确实存在,点击它进入下一页next.get(0).click();//这里可能需要等待页面转换url = driver.getCurrentUrl();//存储当前产品列表页面

I need to go through the list of products:

  • click on it
  • perform some actions on the product page (these actions are simple and already made)
  • go back to the list that I was already on
  • repeat for all the products of that page

After finishing the product list of this current page, I want to be able to click next and perform the same procedure with the products on the next page.

This is the problem: when it reaches the 10 items of the current page, I have no idea how to change to another page and start all over again.

Example code html:

<!DOCTYPE html> 
<html lang="pt-br">
  <head>
    <title>Produtos</title>
    <meta charset="utf-8">
  </head>
  <body>
<div class="srp-actions blue-button"><a class="primary-action-button label" href="">Produto 1</a></div>
<div class="srp-actions blue-button"><a class="primary-action-button label" href="">Produto 2</a></div>
<div class="srp-actions blue-button"><a class="primary-action-button label" href="">Produto 3</a></div>
<div class="srp-actions blue-button"><a class="primary-action-button label" href="">Produto 4</a></div>

    <br/>

<div id="pagination">
        <ul class="pagination">
        <li class="active">1</li>
        <li class="link"><a class="page-link" href="" title="Página 2">2</a></li>
        <li class="link"><a class="page-link" href="" title="Página 2">3</a></li>
        <li class="link"><a class="page-link" href="" title="Página 2">4</a></li>
        <li class="next"><a class="page-link" href="" rel="next" title="Avançar">next</a></li>
        </ul>
</div>
</body>
</html>

Code Java:

int size = driver.findElements(By.className("page-link")).size();
    System.out.println("Numero de paginas : " + size);
    for(int j = 1 ; j < size ; j++) {                
        if (j < 2) {// we don't need to navigate to the first page
            driver.findElement(By.linkText("Avançar >")).click(); // navigate to page number j
        }           

        String pagesearch = driver.getCurrentUrl();


    for(int i=1;i< links.size();i++){

    links= driver.findElements(By.linkText("Produto"));

    WebDriverWait wait3 = new WebDriverWait(driver, 30);
    wait3.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.linkText("Produto")));

    links.get(i).click();

    Thread.sleep(2000); 

解决方案

The basic logic would be:

  1. Go to the first product list page.
  2. Grab all the product links. Navigate to each product page and do stuff.
  3. Go back to the product list page and click Next.

You will repeat 2 & 3 until there is no Next link.

String url = "http://www.example.com"; // the first page of the product list page
driver.get(url);
List<WebElement> next;
while (true)
{
    // wait for and get all the product links
    WebDriverWait wait = new WebDriverWait(driver, 10);
    List<WebElement> productLinks = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.linkText("Produto")));
    // loop through the product links
    for (WebElement productLink : productLinks)
    {
        driver.get(productLink.getAttribute("href")); // navigate to product page
        // do stuff on product page
    }

    // now we're done with all products on this page, go back to the product list page that we were last on
    driver.get(url);

    // look for a Next link
    next = driver.findElements(By.cssSelector("li.next"));
    if (next.isEmpty())
    {
        // Next link DOES NOT exist, exit loop
        break;
    }

    // Next link DOES exists, click it to go to the next page
    next.get(0).click();
    // may need to wait for page transition here
    url = driver.getCurrentUrl(); // store the current product list page

这篇关于使用 selenium java 在多个页面的产品列表中迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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