2016-07-11 38 views
0

我需要經過產品列表:迭代中的幾頁硒Java產品列表

  • 點擊它
  • 進行產品頁面上的一些行動(這些行動是簡單,已經取得)
  • 回到那個我已經上
  • 重複該頁面

的所有產品經過整理列表該當前頁面的產品列表,我希望能夠點擊下一頁,並執行與下一頁上的產品相同的過程。

這就是問題:當它到達當前頁面的10個項目時,我不知道如何切換到另一個頁面並重新開始。

示例代碼的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> 

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

回答

1

的基本邏輯是:

  1. 轉到第一個產品列表頁面。
  2. 抓住所有的產品鏈接。導航到每個產品頁面並做一些事情。
  3. 返回到產品列表頁面,然後單擊下一步。

您將重複2 & 3,直到沒有下一個鏈接。

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