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>
同樣地,對於其他項目
它使用的webdriver wait.Can我們列出 listele = driver.findElements(By.xpath(「// * [@ id中= '定做 塊網格'] /格之前使用Thread.sleep()方法的工作/ UL/DIV [1] /鋰「)); ? –
Preet
@Preet使用'Thread.Sleep()'是一個不好的習慣。你可以做一些Google搜索來看看爲什麼。 'WebDriverWait'是等待的最佳實踐。 – JeffC