在Selenium的div类中选择项目

时间:2018-10-09 00:11:17

标签: selenium

我的HTML来源粘贴在下面。我想从列表框中选择“ TCP IETF”。我该怎么办。

<div class="mat-select-content ng-trigger ng-trigger-fadeInContent" style="opacity: 1;"> 
  <mat-option _ngcontent-c14="" class="mat-option ng-star-inserted" role="option" tabindex="0" 
    id="mat-option-0" aria-selected="false" aria-disabled="false">
    <span class="mat-option-text">TCP - without Syslog</span>
    <div class="mat-option-ripple mat-ripple" mat-ripple=""></div>
  </mat-option>

  <mat-option _ngcontent-c14="" class="mat-option ng-star-inserted" role="option" tabindex="0" 
    id="mat-option-1" aria-selected="false" aria-disabled="false">
    <span class="mat-option-text">UDP - without Syslog</span>
    <div class="mat-option-ripple mat-ripple" mat-ripple=""></div>
  </mat-option>

  <mat-option _ngcontent-c14="" class="mat-option ng-star-inserted" role="option" tabindex="0" 
    id="mat-option-2" aria-selected="false" aria-disabled="false">
    <span class="mat-option-text">TCP BSD Syslog</span>
    <div class="mat-option-ripple mat-ripple" mat-ripple=""></div>
  </mat-option>

  <mat-option _ngcontent-c14="" class="mat-option ng-star-inserted" role="option" tabindex="0" 
    id="mat-option-3" aria-selected="false" aria-disabled="false">
    <span class="mat-option-text">UDP BSD Syslog</span>
    <div class="mat-option-ripple mat-ripple" mat-ripple=""></div>
  </mat-option>

  <mat-option _ngcontent-c14="" class="mat-option ng-star-inserted mat-selected mat-active" role="option" tabindex="0" 
    id="mat-option-4" aria-selected="true" aria-disabled="false">
    <span class="mat-option-text firepath-matching-node">TCP IETF Syslog</span>
    <div class="mat-option-ripple mat-ripple" mat-ripple=""></div>
  </mat-option>

  <mat-option _ngcontent-c14="" class="mat-option ng-star-inserted" role="option" tabindex="0" 
    id="mat-option-5" aria-selected="false" aria-disabled="false">
    <span class="mat-option-text">UDP IETF Syslog</span>
    <div class="mat-option-ripple mat-ripple" mat-ripple=""></div>
  </mat-option>

  <mat-option _ngcontent-c14="" class="mat-option ng-star-inserted" role="option" tabindex="0" 
    id="mat-option-6" aria-selected="false" aria-disabled="false">
    <span class="mat-option-text">TLS IETF Syslog</span>
    <div class="mat-option-ripple mat-ripple" mat-ripple=""></div>
  </mat-option>

  <mat-option _ngcontent-c14="" class="mat-option ng-star-inserted" role="option" tabindex="0" 
    id="mat-option-7" aria-selected="false" aria-disabled="false">
    <span class="mat-option-text">TLS BSD Syslog</span>
    <div class="mat-option-ripple mat-ripple" mat-ripple=""></div>
  </mat-option>

</div>

1 个答案:

答案 0 :(得分:0)

您不清楚要使用哪种语言来运行Selenium,但是我将以C#提供示例,为您提供入门基础。

使用列表框/下拉菜单的重要部分是将基本元素转换为选择元素,然后在列表中选择实际要选择的元素。在这种情况下,我们可以按索引选择。由于我们知道您想要的元素是列表中的第5个元素,因此我们将使用索引4。

    IWebElement listBox = driver.FindElement(By.ClassName("mat-select-content ng-trigger ng-trigger-fadeInContent"));

    var listBoxSelect = new SelectElement(listBox);

    listBoxSelect.SelectByIndex(4);