通过标识同一行中的另一个单元格值来单击行值中的元素

时间:2019-04-10 07:18:52

标签: javascript automation protractor

我必须单击表中所有行共有的元素。但是要注意的是,我只想单击该元素的1个特定行。我需要的行可以在表中的任何位置。因此,我所需要做的就是确定所需行,然后单击该元素。

添加了我已经尝试过的代码,但失败了。

var ClickEdit = element
    .all(by.cssContainingText('.table-element-row'))
    .filter(function(rowElement){
        return rowElement.element(by.css("td h5")).getText().then(function(text){
            return text.trim() == "Company Created by Test Automation"
        });
    })
    .first()
    .element(by.name('action-menu-trigger')).click();

我需要的是:标识具有“由Test Automation创建的”行,然后在同一行中通过name = action-menu-trigger单击该元素。

该表的所有行中都包含有name = action-menu-trigger元素。

<tr _ngcontent-c44="" class="table-element-row mat-row ng-tns-c44-63 ng-star-inserted" mat-row="" role="row">

  <td _ngcontent-c44=""
class="custom-t-user mat-cell cdk-column-responsible mat-column-responsible ng-tns-c44-63 ng-star-inserted"
mat-cell="" role="gridcell">
<app-user-profile-logo _ngcontent-c44="" class="ng-tns-c44-63" shape="circle" size="small" _nghost-c18="">
  <div _ngcontent-c18="" aria-describedby="cdk-describedby-message-20" cdk-describedby-host=""
    class="logo logo-size--small logo-shape--circle d-inline-block m-0 mt-2"
    style="touch-action: none; user-select: none; -webkit-user-drag: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);">
    <div _ngcontent-c18="">
      <div _ngcontent-c18="" class="logo-initial ng-star-inserted">RS</div>
      <img _ngcontent-c18="" class="custom-lg-img-pos ng-star-inserted" data-img-type="no-user"
        data-max-retry="4" data-retry="0"
        src="http://studio.images/demo-designer/images/d374d7560002a04eb20878005a53c1d0df18_4.jpg">
    </div>
  </div>
</app-user-profile-logo>
  </td>

  <td _ngcontent-c44="" class="custom-t-act mat-cell cdk-column-name mat-column-name ng-tns-c44-63 ng-star-inserted"
mat-cell="" role="gridcell">
<h5 _ngcontent-c44="" class="mb-0 pointer" tabindex="0"><b _ngcontent-c44="" class="ng-tns-c44-63">Company
  Created by Test Automation</b>
</h5>
  </td>

  <td _ngcontent-c44="" class="custom-t-act mat-cell cdk-column-owner mat-column-owner ng-tns-c44-63 ng-star-inserted"
mat-cell="" role="gridcell">
<h5 _ngcontent-c44="" class="mb-0 pointer"><b _ngcontent-c44="" class="ng-tns-c44-63">Robert</b></h5>
  </td>

  <td _ngcontent-c44="" class="custom-t-cat mat-cell cdk-column-type mat-column-type ng-tns-c44-63 ng-star-inserted"
mat-cell="" role="gridcell">
<h6 _ngcontent-c44="" class="mb-0 opacity-medium">
  <span _ngcontent-c44="" class="ng-tns-c44-63 ng-star-inserted">
  <span _ngcontent-c44="" class="ng-tns-c44-63 ng-star-inserted"> Owner/Operator </span>
  </span>
</h6>
  </td>

  <td _ngcontent-c44="" class="custom-t-cat mat-cell cdk-column-city mat-column-city ng-tns-c44-63 ng-star-inserted"
mat-cell="" role="gridcell">
<h6 _ngcontent-c44="" class="mb-0 opacity-medium">Dubai</h6>
  </td>

  <td _ngcontent-c44=""
class="custom-t-cat mat-cell cdk-column-country mat-column-country ng-tns-c44-63 ng-star-inserted" mat-cell=""
role="gridcell">
<h6 _ngcontent-c44="" class="mb-0 opacity-medium">United Arab Emirates</h6>
  </td>

  <td _ngcontent-c44=""
class="custom-t-actions mat-cell cdk-column-actions mat-column-actions ng-tns-c44-63 ng-star-inserted"
mat-cell="" role="gridcell">
<button _ngcontent-c44="" class="ng-tns-c44-63 mat-icon-button" aria-haspopup="true" mat-icon-button=""
  name="action-menu-trigger">
  <span class="mat-button-wrapper">
    <mat-icon _ngcontent-c44="" class="inv-icon-size--small mat-icon ng-tns-c44-63 mat-icon-no-color"
      role="img" svgicon="inv-ellipsis-h" aria-hidden="true">
      <svg width="100%" height="100%" viewBox="0 0 17 3" version="1.1" xmlns="http://www.w3.org/2000/svg"
        xmlns:xlink="http://www.w3.org/1999/xlink" fit="" preserveAspectRatio="xMidYMid meet"
        focusable="false">
        <!-- Generator: sketchtool 51.3 (57544) - http://www.bohemiancoding.com/sketch -->
        <title>E1FE60C0-EF43-4EE7-AF32-E28F60D7C746@1,5x</title>
        <desc>Created with sketchtool.</desc>
        <defs></defs>
        <g id="UX-DESIGNER" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
          <g id="ui-03list_02--spec--product-type" transform="translate(-1493.000000, -589.000000)"
            fill="#333333">
            <g id="list/product-type-copy-2" transform="translate(255.000000, 547.000000)">
              <g id="Group-10" transform="translate(185.000000, 18.000000)">
                <g id="package/list">
                  <g id="ico/options" transform="translate(1053.000000, 24.000000)">
                    <g id="Group-4">
                      <circle id="Oval" cx="1.5" cy="1.5" r="1.5"></circle>
                      <circle id="Oval-Copy" cx="8.5" cy="1.5" r="1.5"></circle>
                      <circle id="Oval-Copy-2" cx="15.5" cy="1.5" r="1.5"></circle>
                    </g>
                  </g>
                </g>
              </g>
            </g>
          </g>
        </g>
      </svg>
    </mat-icon>
  </span>
  <div class="mat-button-ripple mat-ripple mat-button-ripple-round" matripple=""></div>
  <div class="mat-button-focus-overlay"></div>
</button>
<mat-menu _ngcontent-c44="" class="ng-tns-c44-63 ng-tns-c9-93" xposition="before">
</mat-menu>
  </td>
</tr>

<tr _ngcontent-c44="" class="table-element-row mat-row ng-tns-c44-63 ng-star-inserted" mat-row="" role="row">
  <td _ngcontent-c44=""
class="custom-t-user mat-cell cdk-column-responsible mat-column-responsible ng-tns-c44-63 ng-star-inserted"
mat-cell="" role="gridcell">
<app-user-profile-logo _ngcontent-c44="" class="ng-tns-c44-63" shape="circle" size="small" _nghost-c18="">
  <div _ngcontent-c18="" aria-describedby="cdk-describedby-message-20" cdk-describedby-host=""
    class="logo logo-size--small logo-shape--circle d-inline-block m-0 mt-2"
    style="touch-action: none; user-select: none; -webkit-user-drag: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);">
    <div _ngcontent-c18="">
      <div _ngcontent-c18="" class="logo-initial ng-star-inserted">RS</div>
      <img _ngcontent-c18="" class="custom-lg-img-pos ng-star-inserted" data-img-type="no-user"
        data-max-retry="4" data-retry="0"
        src="http://studio.images/demo-designer/images/d374d7560002a04eb20878005a53c1d0df18_4.jpg">
    </div>
  </div>
</app-user-profile-logo>
  </td>
  <td _ngcontent-c44="" class="custom-t-act mat-cell cdk-column-name mat-column-name ng-tns-c44-63 ng-star-inserted"
mat-cell="" role="gridcell">
<h5 _ngcontent-c44="" class="mb-0 pointer" tabindex="0"><b _ngcontent-c44="" class="ng-tns-c44-63">Company
  Created by Test Automation</b>
</h5>
  </td>
  <td _ngcontent-c44="" class="custom-t-act mat-cell cdk-column-owner mat-column-owner ng-tns-c44-63 ng-star-inserted"
mat-cell="" role="gridcell">
<h5 _ngcontent-c44="" class="mb-0 pointer"><b _ngcontent-c44="" class="ng-tns-c44-63">Robert</b></h5>
  </td>
  <td _ngcontent-c44="" class="custom-t-cat mat-cell cdk-column-type mat-column-type ng-tns-c44-63 ng-star-inserted"
mat-cell="" role="gridcell">
<h6 _ngcontent-c44="" class="mb-0 opacity-medium">
  <span _ngcontent-c44="" class="ng-tns-c44-63 ng-star-inserted">
  <span _ngcontent-c44="" class="ng-tns-c44-63 ng-star-inserted"> Owner/Operator </span>
  </span>
</h6>
  </td>
  <td _ngcontent-c44="" class="custom-t-cat mat-cell cdk-column-city mat-column-city ng-tns-c44-63 ng-star-inserted"
mat-cell="" role="gridcell">
<h6 _ngcontent-c44="" class="mb-0 opacity-medium">Dubai</h6>
  </td>
  <td _ngcontent-c44=""
class="custom-t-cat mat-cell cdk-column-country mat-column-country ng-tns-c44-63 ng-star-inserted" mat-cell=""
role="gridcell">
<h6 _ngcontent-c44="" class="mb-0 opacity-medium">United Arab Emirates</h6>
  </td>
  <td _ngcontent-c44=""
class="custom-t-actions mat-cell cdk-column-actions mat-column-actions ng-tns-c44-63 ng-star-inserted"
mat-cell="" role="gridcell">
<button _ngcontent-c44="" class="ng-tns-c44-63 mat-icon-button" aria-haspopup="true" mat-icon-button=""
  name="action-menu-trigger">
  <span class="mat-button-wrapper">
    <mat-icon _ngcontent-c44="" class="inv-icon-size--small mat-icon ng-tns-c44-63 mat-icon-no-color"
      role="img" svgicon="inv-ellipsis-h" aria-hidden="true">
      <svg width="100%" height="100%" viewBox="0 0 17 3" version="1.1" xmlns="http://www.w3.org/2000/svg"
        xmlns:xlink="http://www.w3.org/1999/xlink" fit="" preserveAspectRatio="xMidYMid meet"
        focusable="false">
        <!-- Generator: sketchtool 51.3 (57544) - http://www.bohemiancoding.com/sketch -->
        <title>E1FE60C0-EF43-4EE7-AF32-E28F60D7C746@1,5x</title>
        <desc>Created with sketchtool.</desc>
        <defs></defs>
        <g id="UX-DESIGNER" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
          <g id="ui-03list_02--spec--product-type" transform="translate(-1493.000000, -589.000000)"
            fill="#333333">
            <g id="list/product-type-copy-2" transform="translate(255.000000, 547.000000)">
              <g id="Group-10" transform="translate(185.000000, 18.000000)">
                <g id="package/list">
                  <g id="ico/options" transform="translate(1053.000000, 24.000000)">
                    <g id="Group-4">
                      <circle id="Oval" cx="1.5" cy="1.5" r="1.5"></circle>
                      <circle id="Oval-Copy" cx="8.5" cy="1.5" r="1.5"></circle>
                      <circle id="Oval-Copy-2" cx="15.5" cy="1.5" r="1.5"></circle>
                    </g>
                  </g>
                </g>
              </g>
            </g>
          </g>
        </g>
      </svg>
    </mat-icon>
  </span>
  <div class="mat-button-ripple mat-ripple mat-button-ripple-round" matripple=""></div>
  <div class="mat-button-focus-overlay"></div>
</button>
<mat-menu _ngcontent-c44="" class="ng-tns-c44-63 ng-tns-c9-93" xposition="before">
</mat-menu>
  </td>
</tr>

<button _ngcontent-c44="" class="ng-tns-c44-63 mat-icon-button" aria-haspopup="true" mat-icon-button="" name="action-menu-trigger"><span class="mat-button-wrapper"><mat-icon _ngcontent-c44="" class="inv-icon-size--small mat-icon ng-tns-c44-63 mat-icon-no-color" role="img" svgicon="inv-ellipsis-h" aria-hidden="true"><svg width="100%" height="100%" viewBox="0 0 17 3" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" fit="" preserveAspectRatio="xMidYMid meet" focusable="false">
    <!-- Generator: sketchtool 51.3 (57544) - http://www.bohemiancoding.com/sketch -->
    <title>E1FE60C0-EF43-4EE7-AF32-E28F60D7C746@1,5x</title>
    <desc>Created with sketchtool.</desc>
    <defs></defs>
    <g id="UX-DESIGNER" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
        <g id="ui-03list_02--spec--product-type" transform="translate(-1493.000000, -589.000000)" fill="#333333">
            <g id="list/product-type-copy-2" transform="translate(255.000000, 547.000000)">
                <g id="Group-10" transform="translate(185.000000, 18.000000)">
                    <g id="package/list">
                        <g id="ico/options" transform="translate(1053.000000, 24.000000)">
                            <g id="Group-4">
                                <circle id="Oval" cx="1.5" cy="1.5" r="1.5"></circle>
                                <circle id="Oval-Copy" cx="8.5" cy="1.5" r="1.5"></circle>
                                <circle id="Oval-Copy-2" cx="15.5" cy="1.5" r="1.5"></circle>
                            </g>
                        </g>
                    </g>
                </g>
            </g>
        </g>
    </g>
</svg></mat-icon></span><div class="mat-button-ripple mat-ripple mat-button-ripple-round" matripple=""></div><div class="mat-button-focus-overlay"></div></button>

2 个答案:

答案 0 :(得分:0)

如果可以使用XPath,请选择<button>元素,该元素是包含“ Test Automation创建的”文本的元素的后代,请使用以下选择器

//td[contains(., 'Created by Test Automation')]/following-sibling::td//button

答案 1 :(得分:0)

您可以按照以下方式将搜索文本传递到by.cssContainingText()中,然后就无需过滤。

element(by.cssContainingText('tr.table-element-row', 'Company Created by Test Automation'))
.element(by.xpath('./..'))
.element(by.css('button[name="action-menu-trigger"]'))
.click()