Xpath-找到两个相同的元素时如何标记每个元素

时间:2019-11-28 11:09:30

标签: selenium xpath

嗨,我在页面上有两个确切的元素,我想先单击或单击第二个。使用1 [0]时,两个元素被标记为enter image description here

我尝试过的是'// span [contains(text(),“ CTA Button”)]]',但是两个元素都被标记了 还再次标记了“ // span contains(text(), "CTA Button")

when using [1] to grab single argument

<div class="ng-untouched ng-pristine ng-valid ng-star-inserted">

  <!---->


  <!----><!---->
    <mat-slide-toggle class="mat-slide-toggle mat-accent ng-untouched ng-pristine ng-valid ng-star-inserted" id="mat-slide-toggle-6"><label class="mat-slide-toggle-label"><div class="mat-slide-toggle-bar"><input class="mat-slide-toggle-input cdk-visually-hidden" type="checkbox" id="mat-slide-toggle-6-input" tabindex="0"><div class="mat-slide-toggle-thumb-container" style="touch-action: none; user-select: none; -webkit-user-drag: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><div class="mat-slide-toggle-thumb"></div><div class="mat-slide-toggle-ripple mat-ripple" mat-ripple=""></div></div></div><span class="mat-slide-toggle-content">
      CTA Button
    </span></label></mat-slide-toggle>
    <!---->
</div>

1 个答案:

答案 0 :(得分:2)

这是要查询第一个元素:(//span[contains(text(), "CTA Button")])[1]

这是要查询第二个元素:(//span[contains(text(), "CTA Button")])[2]