Selenium webdriver 我得到的列表大小为0,但当我使用x路径帮助器验证它时,它显示了正确的项目计数

Selenium webdriver 我得到的列表大小为0,但当我使用x路径帮助器验证它时,它显示了正确的项目计数,selenium-webdriver,Selenium Webdriver,我试图找出函授课程页面上的项目数量。所有项目都包含相同的类名。它总是返回0大小,但是我使用的xpath在使用xpath帮助程序验证它时给出了正确的cont。这里是我的selenium代码: @Test(priority=2) public void AddItems() throws InterruptedException driver.findElement(By.linkText("CORRESPONDENCE COURSES")).click(); List <WebEle

我试图找出函授课程页面上的项目数量。所有项目都包含相同的类名。它总是返回0大小,但是我使用的xpath在使用xpath帮助程序验证它时给出了正确的cont。这里是我的selenium代码:

 @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());    
@测试(优先级=2)
public void AddItems()引发InterruptedException
driver.findElement(By.linkText(“函授课程”))。单击();
List listle=driver.findElements(By.xpath(“/*[@id='custom-
块网格“]/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>

    • SSC函授课程(印地语) 4,499 3,599 添加到购物车
    • SSC函授课程(印地语) 4,499 3,599 添加到购物车
    • SSC函授课程(印地语) 4,499 3,599 添加到购物车

类似地,对于其他项目

,如果在浏览器中测试时它工作,但在脚本运行时不工作,则很可能是时间问题。代码运行速度很快,可能在页面完成加载之前返回0。解决此问题的方法是添加等待,
WebDriverWait

下面是示例代码。wait返回等待的元素集合,这样您就可以像处理代码一样将其粘贴到
列表中,然后打印集合的
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());
By locator=By.cssSelector(“文章”);
//By locator=By.xpath(“/*[@id='custom-block-grid']/div/ul/div[1]/li”);
列表课程=新的WebDriverWait(驱动程序,10)。直到(预期条件。由(定位器)分类的所有元素的可见性);
System.out.println(courses.size());

还有一件事。。。由于您只是在计算课程的数量,因此这些课程用
article
标记表示,这样您就可以只计算那些使定位器变得简单的课程了。

使用WebDriver wait可以工作。我们可以在List listele=driver.findElements(By.xpath(“/*[@id='custom-block grid']/div/ul/div[1]/li”);之前使用Thread.sleep()吗@Preet使用
Thread.Sleep()
是一种不好的做法。你可以在谷歌上搜索一下原因
WebDriverWait
是等待的最佳实践。