2017-09-05 43 views
0

我試圖找出項目數的計數一個網頁,函授教學上驗證它的各個項目的權數。所有的項目都包含相同的類名稱。它始終返回0大小,但是我使用的xpath在使用xpath幫助程序驗證它時給出了正確的連續值。這是我的硒代碼在這裏:我得到一個列表的大小爲0但它顯示了當我使用X路徑幫手

@Test(priority=2) 
public void AddItems() throws InterruptedException 
driver.findElement(By.linkText("CORRESPONDENCE COURSES")).click(); 
List <WebElement> listele = driver.findElements(By.xpath("//*[@id='custom- 
block-grid']/div/ul/div[1]/li")); 

System.out.println(listele.size());  

HTML是:

<div class="small-9 columns product-card left" role="main" id="card-width" style="padding-left: 10px; float: right !important"> 
    <article> 
    <ul class="small-block-grid-2 cp-inline-block padding-card ng-scope" ng- repeat="data in testseriesData" style=""> 
     <li id="custom-block-grid" class=""> 
     <div class="panel card-background card-shadow"> 
      <ul class="no-bullet"> 
      <div class="image-card-container"> 
       <li class="card-pointer image-align-card" ui-sref="description({cName: 'books', id: data.id})" href="#/books/description/id=750"> 
       <img ng src="https://d2fldgtygklyv6.cloudfront.net/SSC_Corr_Course_with_test_HINDI_1500195234532.png" class="img-card-dim" src="https://d2fldgtygklyv6.cloudfront.net/SSC_Corr_Course_with_test_HINDI_1500195234532.png"> 
       </li> 
      </div> 
      <span class="line-clamp card-pointer label-card title-border-top ng-binding" ui-sref="description({cName: 'books', id: data.id})" href="#/books/description/id=750">SSC Correspondence Course (HINDI MEDIUM)</span> 
      <div class="price-field"> 
       <del class="fade ng-binding"><i class="icon-rupee icon-rupee-buy-card" aria-hidden="true"></i>4,499</del> 
       <span class="cp-float-right selling-price ng-binding"><i class="icon-rupee icon-rupee-buy-card" aria-hidden="true"></i>3,599</span> 
      </div> 
      <div class="add-to-cart listing-add-to-cart margin-force-zero sf-button-secondary" ng-click="addToCart(data)" role="button" tabindex="0"> 
       <span class="icon-cart icon-cart-buy-card"></span> 
       <span>ADD TO CART</span> 
      </div> 
      </ul> 
     </div> 
     </li> 
    </ul> 
    </article> 
</div> 
<div class="small-9 columns product-card left" role="main" id="card-width" style="padding-left: 10px; float: right !important"> 
    <article> 
    <ul class="small-block-grid-2 cp-inline-block padding-card ng-scope" ng- repeat="data in testseriesData" style=""> 
     <li id="custom-block-grid" class=""> 
     <div class="panel card-background card-shadow"> 
      <ul class="no-bullet"> 
      <div class="image-card-container"> 
       <li class="card-pointer image-align-card" ui-sref="description({cName: 'books', id: data.id})" href="#/books/description/id=750"> 
       <img ng src="https://d2fldgtygklyv6.cloudfront.net/SSC_Corr_Course_with_test_HINDI_1500195234532.png" class="img-card-dim" src="https://d2fldgtygklyv6.cloudfront.net/SSC_Corr_Course_with_test_HINDI_1500195234532.png"> 
       </li> 
      </div> 
      <span class="line-clamp card-pointer label-card title-border-top ng-binding" ui-sref="description({cName: 'books', id: data.id})" href="#/books/description/id=750">SSC Correspondence Course (HINDI MEDIUM)</span> 
      <div class="price-field"> 
       <del class="fade ng-binding"><i class="icon-rupee icon-rupee-buy-card" aria-hidden="true"></i>4,499</del> 
       <span class="cp-float-right selling-price ng-binding"><i class="icon-rupee icon-rupee-buy-card" aria-hidden="true"></i>3,599</span> 
      </div> 
      <div class="add-to-cart listing-add-to-cart margin-force-zero sf-button-secondary" ng-click="addToCart(data)" role="button" tabindex="0"> 
       <span class="icon-cart icon-cart-buy-card"></span> 
       <span>ADD TO CART</span> 
      </div> 
      </ul> 
     </div> 
     </li> 
    </ul> 
    </article> 
</div> 
<div class="small-9 columns product-card left" role="main" id="card-width" style="padding-left: 10px; float: right !important"> 
    <article> 
    <ul class="small-block-grid-2 cp-inline-block padding-card ng-scope" ng- repeat="data in testseriesData" style=""> 
     <li id="custom-block-grid" class=""> 
     <div class="panel card-background card-shadow"> 
      <ul class="no-bullet"> 
      <div class="image-card-container"> 
       <li class="card-pointer image-align-card" ui-sref="description({cName: 'books', id: data.id})" href="#/books/description/id=750"><img ng src="https://d2fldgtygklyv6.cloudfront.net/SSC_Corr_Course_with_test_HINDI_1500195234532.png" class="img-card-dim" src="https://d2fldgtygklyv6.cloudfront.net/SSC_Corr_Course_with_test_HINDI_1500195234532.png"> 
       </li> 
      </div> 
      <span class="line-clamp card-pointer label-card title-border-top ng-binding" ui-sref="description({cName: 'books', id: data.id})" href="#/books/description/id=750">SSC Correspondence Course (HINDI MEDIUM)</span> 
      <div class="price-field"> 
       <del class="fade ng-binding"><i class="icon-rupee icon-rupee-buy-card" aria-hidden="true"></i>4,499</del> 
       <span class="cp-float-right selling-price ng-binding"><i class="icon-rupee icon-rupee-buy-card" aria-hidden="true"></i>3,599</span> 
      </div> 
      <div class="add-to-cart listing-add-to-cart margin-force-zero sf-button-secondary" ng-click="addToCart(data)" role="button" tabindex="0"> 
       <span class="icon-cart icon-cart-buy-card"></span> 
       <span>ADD TO CART</span> 
      </div> 
      </ul> 
     </div> 
     </li> 
    </ul> 
    </article> 
</div> 

同樣地,對於其他項目

回答

0

如果它的工作,當你測試它在瀏覽器中而不是當你的腳本運行,這很可能是時間問題。代碼運行速度快,並可能在頁面加載完成之前返回0。解決這個問題的方法加一個等待,WebDriverWait

實施例的代碼如下。等待返回的元素集合等待,以便您可以然後堅持,在一個List,爲您與您的代碼一樣,然後打印集合的size()。我將變量的名稱更改爲更具描述性的courses,而不是listele。它將幫助您和其他人更輕鬆地閱讀和理解您的代碼。

By locator = By.cssSelector("article"); 
// By locator = By.xpath("//*[@id='custom-block-grid']/div/ul/div[1]/li"); 
List<WebElement> courses = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(locator)); 
System.out.println(courses.size()); 

另一件事......因爲你只是計算的課程的數量,那些標註有article標記,以便你可以指望那些使你的定位簡單。

+0

它使用的webdriver wait.Can我們列出 listele = driver.findElements(By.xpath(「// * [@ id中= '定做 塊網格'] /格之前使用Thread.sleep()方法的工作/ UL/DIV [1] /鋰「)); ? – Preet

+0

@Preet使用'Thread.Sleep()'是一個不好的習慣。你可以做一些Google搜索來看看爲什麼。 'WebDriverWait'是等待的最佳實踐。 – JeffC

相關問題