我试图找出一个页面上的项目数量是通信课程。所有项目都包含相同的类名称。它一直返回0大小,但是当我使用xpath helper验证它时,我使用的xpath给出正确的cont。这是我的硒代码:
@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 :(得分:1)
如果它在浏览器中测试时有效,但在脚本运行时没有,那么很可能是时间问题。代码运行速度很快,可能在页面加载完成之前返回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
标签表示,这样你就可以计算那些使你的定位器变得简单的东西。