如何从Seleniumjava中选择<md-select>下拉列表?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (327)

我正在尝试自动化一个具有类型下拉的应用程序,已经尝试过以下所有可能的组合,但无法从下拉列表中选择一个元素。

driver.findElement(By.xpath("//span[@aria-label=\"Phone to call with\"]"));

无法通过ID找到XPath

下面是html代码

<md-select ng-model="ctrl.selectedLinkedPhone" class="ng-pristine ng-valid ng-empty ng-touched" tabindex="0" aria-disabled="false" role="listbox" aria-expanded="false" aria-multiselectable="false" id="select_26" aria-invalid="false" aria-label="
        Phone to call with
      " style=""><md-select-value class="md-select-value md-select-placeholder" id="select_value_label_25"><span>
        Phone to call with
      </span><span class="md-select-icon" aria-hidden="true"></span></md-select-value><div class="md-select-menu-container" aria-hidden="true" role="presentation" id="select_container_27"><md-select-menu role="presentation" class="_md"><md-content class="_md">
        <!----><md-option ng-disabled="!ctrl.isLinkedPhoneCurrentlyVerified(linkedPhone)" ng-repeat="linkedPhone in ctrl.getLinkedPhones()" ng-value="linkedPhone" tabindex="0" class="md-ink-ripple" role="option" aria-selected="false" id="select_option_29" aria-checked="true" aria-disabled="false" value="[object Object]" style=""><div class="md-text">
          Phone 1
        </div></md-option><!----><md-option ng-disabled="!ctrl.isLinkedPhoneCurrentlyVerified(linkedPhone)" ng-repeat="linkedPhone in ctrl.getLinkedPhones()" ng-value="linkedPhone" tabindex="0" class="md-ink-ripple" role="option" aria-selected="false" id="select_option_30" aria-checked="true" aria-disabled="false" value="[object Object]" style=""><div class="md-text">
          Phone 2
        </div></md-option><!----><md-option ng-disabled="!ctrl.isLinkedPhoneCurrentlyVerified(linkedPhone)" ng-repeat="linkedPhone in ctrl.getLinkedPhones()" ng-value="linkedPhone" tabindex="0" class="md-ink-ripple" role="option" aria-selected="false" id="select_option_31" aria-checked="true" aria-disabled="false" value="[object Object]" style=""><div class="md-text">
          Hangouts
        </div></md-option><!---->
      </md-content></md-select-menu></div></md-select>
提问于
用户回答回答于

尝试使用下面的XPath

driver.findElement(By.xpath("//span[contains(@aria-label,Phone to call with']"));

用户回答回答于

使用xpath driver.findElement(By.xpath("//span[contains(@aria-label,'Phone to call with')]"));

扫码关注云+社区

领取腾讯云代金券