【问题标题】:Locating elements in <li> using selenium webdriver with java使用带有 java 的 selenium webdriver 定位 <li> 中的元素
【发布时间】:2016-11-25 05:50:20
【问题描述】:

我需要找到每个项目并单击它。 我厌倦了这个:

List<WebElement> allTournaments = driver.findElements(By.xpath("//*[@id='main-section']/div/div/section/div[1]/ul/li/div/div[1]/div"));
        for (WebElement tournament : allTournaments) {
tournament.click();
}

但收到错误“元素不可见”

第一个元素的xpath是 //[@id='main-section']/div/div/section/div1/ul/li1/div/div1/ div 第二个元素的 xpath 是 //[@id='main-section']/div/div/section/div1/ul/li[2]/div/div1/div 明智的..

有人可以帮忙吗?

HTML:

   <div class="header-toggle-area">
<div class="header-notification-area text-right ng-scope" ng-controller="notificationController">
</header>
<!--HEADER ENDS-->
<div class="content-wrap" style="padding-top: 60px;">
<aside id="main-sidebar">
<!--MAIN SIDEBAR ENDS-->
<main id="main-section">
<div class="main-content">
<div class="ng-scope" ng-controller="NewTournamentsController as tournaments">
<header class="member-title dt-title-main">
<section class="dashboard-tournaments-listing">
<div class="" ng-show="tournaments.Page==1">
<ul class="row dashboard-listing">
<!-- ngRepeat: running in tournaments.runningTournaments -->
<li class="dashboard-listing-items ng-scope" ng-repeat="running in tournaments.runningTournaments" style="">
<div class="dtslider-block">
<div class="dtslider-content">
<img ng-src="../../category/categoryDefault.jpg" alt="slider" src="../../category/categoryDefault.jpg"/>
<div class="hover-contents">
<div class="hc-timer">
<p>   Tournament ends in </p>
<div class="hct-ticker ng-binding">   04:30 - 26/11/16 </div>
</div>
<div class="hc-details">
<div class="hcd-block ng-hide" ng-show="running.JoinedPlayers>0">
<ul class="hc-icons">
</div>
</div>
</div>

【问题讨论】:

  • 把相关的HTML代码也贴出来!!谢谢!!
  • HTML 代码添加问题
  • 您要点击哪个元素?整个 DIV 或 DIV 内的任何特定元素。
  • 在那个 div 中有 4 个盒子,里面有一个球员的形象。每个方框代表一个问答游戏。我想点击每个框。
  • 而不是单击 div 元素,而是在 div 中找到图像标签的 xpath 并单击它。问题中提到的 HTML 块没有提供的图像的完整代码。

标签: java selenium-webdriver


【解决方案1】:

检查每个元素是否可见和可点击如下:

for (WebElement tournament : allTournaments) {
  if (tournament.isDisplayed()
      && ExpectedConditions.elementToBeClickable(tournament).equals(true)) {
    tournament.click();
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多