如何使用html选择一系列列表项

时间:2018-09-18 05:44:12

标签: java html selenium-webdriver testng html-lists

我的应用程序中有一个列表。该列表被分为多个部分,例如“当前查看为”,“最近查看”和“工作人员”等。所有列表元素都共享相同的类,因此例如在最近查看的部分中标识列表元素并不是直截了当的

我尝试了以下操作:

RecentlyViewed(".list-group-item:nth-of-type(3)"
        .concat("~ .list-group-item:nth-of-type(4)"))

但是这似乎只返回1个列表项,而不是两个都返回。

是否有指针同时返回列表中的第三项和第四项?

顺便说一句,上面的定位符被传递给如下方法:

private WebElement sectionSearch(String locator, String empName) {
    return wait.andGetAllElements(locator).stream()
        .filter(ele -> ele.getAttribute("data-empno").equals(empName))
        .findFirst()
        .orElseThrow(() -> new RuntimeException("Could not find employee: ".concat(empName)
          .concat(" in element : ").concat(locator)));
  }

在传递给搜索功能的列表项上我可能出错的任何想法吗?

html如下:

<div id="context-selector-search-results" class="list-group list-group-menu"><h4 class="text-muted">You are signed in as...</h4><li class="list-group-item list-group-item-split">

            <a href="" class="list-group-item-link" data-empno="3000" data-url="api/context/select/3000"><div class="media">
          <div class="media-left">
            <img class="context-selector-search-results-item-image media-object rounded-circle img-avatar" alt="Avatar" src="file/photo/T000~|~3000~|~2915483350193499~|~GIF~|~1~|~ok.gif" data-holder-rendered="true">
          </div>
          <div class="media-body">

            <small class="context-selector-search-results-item-empno">3000</small>
            <br>
            <strong class="context-selector-search-results-item-display-name">MANAGER3000, Mary G</strong>
            <br>

            <small class="context-selector-search-results-item-unit">Manufacturing Division</small><br>

          </div>
        </div></a>
        </li><h4 class="text-muted">Currently viewing...</h4><li class="list-group-item list-group-item-split active active-warning">

            <a href="" class="list-group-item-link" data-empno="5029" data-url="api/context/select/5029"><div class="media">
          <div class="media-left">
            <img class="context-selector-search-results-item-image media-object rounded-circle img-avatar" alt="Avatar" src="assets/ui/img/avatar.png" data-holder-rendered="true">
          </div>
          <div class="media-body">

            <small class="context-selector-search-results-item-empno">5029</small>
            <br>
            <strong class="context-selector-search-results-item-display-name">EMPLOYEE, Earl</strong>
            <br>

            <small class="context-selector-search-results-item-unit">Manufacturing Division</small><br>

          </div>
        </div></a>
        </li><h4 class="text-muted">Recently viewed...</h4><li class="list-group-item list-group-item-split">

            <a href="" class="list-group-item-link" data-empno="5029" data-url="api/context/select/5029"><div class="media">
          <div class="media-left">
            <img class="context-selector-search-results-item-image media-object rounded-circle img-avatar" alt="Avatar" src="assets/ui/img/avatar.png" data-holder-rendered="true">
          </div>
          <div class="media-body">

            <small class="context-selector-search-results-item-empno">5029</small>
            <br>
            <strong class="context-selector-search-results-item-display-name">EMPLOYEE, Earl</strong>
            <br>

            <small class="context-selector-search-results-item-unit">Manufacturing Division</small><br>

          </div>
        </div></a>
        </li><li class="list-group-item list-group-item-split">

            <a href="" class="list-group-item-link" data-empno="5027" data-url="api/context/select/5027"><div class="media">
          <div class="media-left">
            <img class="context-selector-search-results-item-image media-object rounded-circle img-avatar" alt="Avatar" src="assets/ui/img/avatar.png" data-holder-rendered="true">
          </div>
          <div class="media-body">

            <small class="context-selector-search-results-item-empno">5027</small>
            <br>
            <strong class="context-selector-search-results-item-display-name">EMPLOYEE, Earlene</strong>
            <br>

            <small class="context-selector-search-results-item-unit">Manufacturing Division</small><br>

          </div>
        </div></a>
        </li><h4 class="text-muted">My staff</h4><li class="list-group-item list-group-item-split">
            <a href="" class="list-group-item-more" data-display-name="JETSON, Jane" data-expand-id="E~4112016">
          <span class="tag tag-pill tag-primary hidden-xs-down float-xs-right">20</span>
        </a>
            <a href="" class="list-group-item-link" data-empno="4112016" data-url="api/context/select/4112016"><div class="media">
          <div class="media-left">
            <img class="context-selector-search-results-item-image media-object rounded-circle img-avatar" alt="Avatar" src="file/photo/T000~|~4112016~|~2915756609048099~|~PNG~|~1~|~janejetson.png" data-holder-rendered="true">
          </div>
          <div class="media-body">

            <small class="context-selector-search-results-item-empno">4112016</small>
            <br>
            <strong class="context-selector-search-results-item-display-name">JETSON, Jane</strong>
            <br>

            <small class="context-selector-search-results-item-unit">Manufacturing Division</small><br>

          </div>
        </div></a>
        </li><li class="list-group-item list-group-item-split">
            <a href="" class="list-group-item-more" data-display-name="PERSON130218, Eric" data-expand-id="E~130218">
          <span class="tag tag-pill tag-primary hidden-xs-down float-xs-right">20</span>
        </a>
            <a href="" class="list-group-item-link" data-empno="130218" data-url="api/context/select/130218"><div class="media">
          <div class="media-left">
            <img class="context-selector-search-results-item-image media-object rounded-circle img-avatar" alt="Avatar" src="assets/ui/img/avatar.png" data-holder-rendered="true">
          </div>
          <div class="media-body">

            <small class="context-selector-search-results-item-empno">130218</small>
            <br>
            <strong class="context-selector-search-results-item-display-name">PERSON130218, Eric</strong>
            <br>

            <small class="context-selector-search-results-item-unit">Manufacturing Division</small><br>

          </div>
        </div></a>
        </li><li class="list-group-item list-group-item-split">
            <a href="" class="list-group-item-more" data-display-name="ROBOTS, Eric" data-expand-id="E~3001">
          <span class="tag tag-pill tag-primary hidden-xs-down float-xs-right">20</span>
        </a>
            <a href="" class="list-group-item-link" data-empno="3001" data-url="api/context/select/3001"><div class="media">
          <div class="media-left">
            <img class="context-selector-search-results-item-image media-object rounded-circle img-avatar" alt="Avatar" src="assets/ui/img/avatar.png" data-holder-rendered="true">
          </div>
          <div class="media-body">

            <small class="context-selector-search-results-item-empno">3001</small>
            <br>
            <strong class="context-selector-search-results-item-display-name">ROBOTS, Eric</strong>
            <br>

            <small class="context-selector-search-results-item-unit">Manufacturing Division</small><br>

          </div>
        </div></a>
        </li><li class="list-group-item list-group-item-split">
            <a href="" class="list-group-item-more" data-display-name="SNODGRASS, Harold" data-expand-id="E~3003">
          <span class="tag tag-pill tag-primary hidden-xs-down float-xs-right">20</span>
        </a>
            <a href="" class="list-group-item-link" data-empno="3003" data-url="api/context/select/3003"><div class="media">
          <div class="media-left">
            <img class="context-selector-search-results-item-image media-object rounded-circle img-avatar" alt="Avatar" src="assets/ui/img/avatar.png" data-holder-rendered="true">
          </div>
          <div class="media-body">

            <small class="context-selector-search-results-item-empno">3003</small>
            <br>
            <strong class="context-selector-search-results-item-display-name">SNODGRASS, Harold</strong>
            <br>

            <small class="context-selector-search-results-item-unit">Manufacturing Division</small><br>

          </div>
        </div></a>
        </li><li class="list-group-item list-group-item-split">
            <a href="" class="list-group-item-more" data-display-name="SNODGRASS, Harold" data-expand-id="E~3004">
          <span class="tag tag-pill tag-primary hidden-xs-down float-xs-right">20</span>
        </a>
            <a href="" class="list-group-item-link" data-empno="3004" data-url="api/context/select/3004"><div class="media">
          <div class="media-left">
            <img class="context-selector-search-results-item-image media-object rounded-circle img-avatar" alt="Avatar" src="assets/ui/img/avatar.png" data-holder-rendered="true">
          </div>
          <div class="media-body">

            <small class="context-selector-search-results-item-empno">3004</small>
            <br>
            <strong class="context-selector-search-results-item-display-name">SNODGRASS, Harold</strong>
            <br>

            <small class="context-selector-search-results-item-unit">Manufacturing Division</small><br>

          </div>
        </div></a>
        </li><li class="list-group-item list-group-item-split">
            <a href="" class="list-group-item-more" data-display-name="VON DA EMPLOYEE, Eric" data-expand-id="E~5031">
          <span class="tag tag-pill tag-primary hidden-xs-down float-xs-right">20</span>
        </a>
            <a href="" class="list-group-item-link" data-empno="5031" data-url="api/context/select/5031"><div class="media">
          <div class="media-left">
            <img class="context-selector-search-results-item-image media-object rounded-circle img-avatar" alt="Avatar" src="file/photo/T000~|~2031~|~2915405557111499~|~JPEG~|~1~|~0A1BE6FA-43C4-4E85-A110-9F797A9525E6.jpeg" data-holder-rendered="true">
          </div>
          <div class="media-body">

            <small class="context-selector-search-results-item-empno">5031</small>
            <br>
            <strong class="context-selector-search-results-item-display-name">VON DA EMPLOYEE, Eric</strong>
            <br>

            <small class="context-selector-search-results-item-unit">Manufacturing Division</small><br>

          </div>
        </div></a>
        </li><li class="list-group-item list-group-item-split">
            <a href="" class="list-group-item-more" data-display-name="WAYNE, Bruce" data-expand-id="E~80000">
          <span class="tag tag-pill tag-primary hidden-xs-down float-xs-right">20</span>
        </a>
            <a href="" class="list-group-item-link" data-empno="80000" data-url="api/context/select/80000"><div class="media">
          <div class="media-left">
            <img class="context-selector-search-results-item-image media-object rounded-circle img-avatar" alt="Avatar" src="file/photo/T000~|~80000~|~2915797423381199~|~JPG~|~1~|~bruce-wayne_197x282.jpg" data-holder-rendered="true">
          </div>
          <div class="media-body">

            <small class="context-selector-search-results-item-empno">80000</small>
            <br>
            <strong class="context-selector-search-results-item-display-name">WAYNE, Bruce</strong>
            <br>

            <small class="context-selector-search-results-item-unit">Manufacturing Division</small><br>

          </div>
        </div></a>
        </li></div>

2 个答案:

答案 0 :(得分:0)

我设法使用以下xpath解决了问题:

//h4[contains(text(),'My staff')]/following::a[@*[starts-with(name(), 'data-em')]

答案 1 :(得分:-1)

One way you could do it is by using XPath. If you pass in your empno to a function and then insert that into the XPath below, it will return the first h4 preceding the found element. You can then match that with what you are expecting, etc.

//a[@data-empno='4112016']/preceding::h4

This returns 'My staff'

You would insert your variable like

"//a[@data-empno='"+ empno + "']/preceding::h4"