首页 > 解决方案 > 如果表中的一行在 x 和 y 列中具有特定模式的值,则查找网络定位器(css​​ 或 xpath)

问题描述

我有一个网站页面可以使用 Selenium 进行自动化测试。有一个包含多行的表。我有兴趣单击一行,其中列 x 具有值“Ready”和列,y 具有“userId”和日期“mm/dd/yyyy”。顺便说一句,所有的行和单元格都是由<div>元素组成的

我能够找到具有值“Ready”的单元格,因为 // div[text()='Ready'] 它在 N 行中返回 n 个这样的单元格。可能我可以通过调用将这些行作为列表获取

WebDriver.getElements("//div[text()='Ready'])

但我还有进一步的调查要做。就像我需要在同一行中使用“用户 ID”字符串和日期“mm/dd/yyyy”在同一单元格中找到其他单元格。

第二部分是我不清楚的,如何!

任何帮助,请!这是我的 html 标记。

<div role="row" row-index="3" row-id="6" comp-id="50" class="ag-row ag-row-odd ag-row-level-0 ag-row--ready ag-row-focus" style="height: 84px; transform: translateY(252px);  ">
  <div tabindex="-1" role="gridcell" comp-id="51" col-id="name" class="ag-cell ag-cell-not-inline-editing ag-cell-with-height ag-cell-no-focus tu-cursor-pointer ag-cell-value ag-column-hover" style="width: 674px; left: 0px; ">
    <ng-component _nghost-c1="" class="center-table">
      <div _ngcontent-c1="" class="extract-name center-table__cell">
        <div _ngcontent-c1="" class="font-bold">PRASAD-FEB-04-215PM-EXTRACT_TEST</div>
        <div _ngcontent-c1="" class="extract-name__description"></div>
      </div>
    </ng-component>
  </div>
  <div tabindex="-1" role="gridcell" comp-id="52" col-id="0" class="ag-cell ag-cell-not-inline-editing ag-cell-with-height font-bold ag-cell-value ag-cell-focus" style="width: 200px; left: 674px; ">**`Ready`**</div>
  <div tabindex="-1" role="gridcell" comp-id="53" col-id="createDate" class="ag-cell ag-cell-not-inline-editing ag-cell-with-height ag-cell-no-focus ag-cell-value" title="2019-02-04 20:19:51.528" style="width: 200px; left: 874px; ">
    <ng-component>**02/04/2019**</ng-component>
  </div>
  <div tabindex="-1" role="gridcell" comp-id="54" col-id="lastRun" class="ag-cell ag-cell-not-inline-editing ag-cell-with-height ag-cell-no-focus tu-cursor-pointer ag-cell-value" style="width: 200px; left: 1074px; ">
    <ng-component _nghost-c2="" class="center-table">
      <!---->
      <div _ngcontent-c2="" class="last-run center-table__cell">
        <div _ngcontent-c2="">**`pnutala`**</div>
        <div _ngcontent-c2="">**`02/04/2019`**</div>
      </div>
    </ng-component>
  </div>
  <div tabindex="-1" role="gridcell" comp-id="55" col-id="1" class="ag-cell ag-cell-not-inline-editing ag-cell-with-height ag-cell-no-focus tu-cursor-pointer ag-cell-value" style="width: 160px; left: 1274px; ">
      <i class="fa fa-copy"></i> <span>Clone</span>
  </div>
  <div tabindex="-1" role="gridcell" comp-id="56" col-id="params" class="ag-cell ag-cell-not-inline-editing ag-cell-with-height ag-cell-no-focus ag-cell-value" style="width: 387px; left: 1434px; ">
    <dhu-preview-list>
      <dhu-preview-info _nghost-c3="" class="center-table">
        <!----><!----><!----><!----><!----><!----><!---->
        <div _ngcontent-c3="" class="center-table__cell">
          <div _ngcontent-c3="">~4,000,000</div>
          <div _ngcontent-c3="">JUN 2016</div>
        </div>
        <!---->
        <div _ngcontent-c3="" class="center-table__cell">
           <button _ngcontent-c3="" cdk-overlay-origin="" class="no-border pull-right" type="button"><i _ngcontent-c3="" aria-hidden="true" class="fa tufa-angle-down"></i></button>
        </div>
        <!---->
      </dhu-preview-info>
    </dhu-preview-list>
  </div>
</div>

标签: seleniumselenium-webdriverxpathcss-selectors

解决方案


这里 xpath 示例包含“就绪”文本:

//div[@role='row' and ./div[contains(.,'Ready')]]
//div[contains(.,'Ready')]/ancestor::div[@role='row'][1]
//div[@col-id='0' and contains(.,'Ready')]/ancestor::div[@role='row'][1]

更新单元格中的用户名和日期lastRun

//div[@role='row' and ./div[contains(.,'Ready')] and ./div[@col-id='lastRun' and contains(.,'pnutala') and contains(.,'02/04/2019')]]

Status是行中的第 2 列,而 是行中UserId的第 4 列,使用列索引更严格地查找行。

//find row by status and userid
//div[@role='row' and ./div[2][contains(., 'Ready')] and ./div[4][contains(., 'pnutala')]]

// find row by status and userid and date
//div[@role='row' and ./div[2][contains(., 'Ready')] and ./div[4][contains(., 'pnutala')][contains(.,'02/04/2019')]]

推荐阅读