使用Md-select时,如何在量角器中下拉列表?

时间:2018-07-19 11:54:57

标签: javascript angularjs selenium protractor

我正在尝试从下拉列表中选择一个选项,但无法找到定位器,我尝试了许多不同的操作,但这没有帮助。

<md-select ng-model="card.type" name="type" aria-label="Select card type" ng-change="$ctrl.onCardSelectionChange(card);$ctrl.onChange(true)" cmtr-no-multiple-bahn-cards="" required="" class="ng-pristine ng-empty ng-invalid ng-invalid-required ng-valid-cmtr-no-multiple-bahn-cards ng-touched" tabindex="0" aria-disabled="false" role="listbox" aria-expanded="false" aria-multiselectable="false" id="select_2162" aria-invalid="true" aria-required="true" aria-owns="select_container_2163" style=""><md-select-value class="md-select-value md-select-placeholder" id="select_value_label_2161"><span>Type</span><span class="md-select-icon" aria-hidden="true"></span></md-select-value></md-select>
<div class="md-select-menu-container md-active md-clickable" aria-hidden="false" role="presentation" id="select_container_2163" style="display: block; left: 52px; top: 137px; min-width: 295.797px; font-size: 16px;"><md-select-menu role="presentation" class="_md" style="transform-origin: 147.898px 8px 0px;"><md-content class="_md">
          <!----><md-option ng-repeat="loyaltyCard in ::$ctrl.loyaltyCards" ng-value="::loyaltyCard.value" tabindex="0" class="md-ink-ripple" role="option" aria-selected="false" id="select_option_2166" aria-checked="true" value="blue_biz"><div class="md-text">
            Blue Biz (AF/KL)
          </div></md-option><!----><md-option ng-repeat="loyaltyCard in ::$ctrl.loyaltyCards" ng-value="::loyaltyCard.value" tabindex="0" class="md-ink-ripple" role="option" aria-selected="false" id="select_option_2167" aria-checked="true" value="partner_plus_benefit_lh"><div class="md-text">
            Partner Plus Benefit (LH)
          </div></md-option><!----><md-option ng-repeat="loyaltyCard in ::$ctrl.loyaltyCards" ng-value="::loyaltyCard.value" tabindex="0" class="md-ink-ripple" role="option" aria-selected="false" id="select_option_2168" aria-checked="true" value="onbusiness_ba_aa_ib"><div class="md-text">
            OnBusiness (BA/AA/IB)
          </div></md-option><!----><md-option ng-repeat="loyaltyCard in ::$ctrl.loyaltyCards" ng-value="::loyaltyCard.value" tabindex="0" class="md-ink-ripple" role="option" aria-selected="false" id="select_option_2169" aria-checked="true" value="SK"><div class="md-text">
            SAS Credit (SK)
          </div></md-option><!----><md-option ng-repeat="loyaltyCard in ::$ctrl.loyaltyCards" ng-value="::loyaltyCard.value" tabindex="0" class="md-ink-ripple" role="option" aria-selected="false" id="select_option_2170" aria-checked="true" value="AY"><div class="md-text">
            Finnair Plus (AY)
          </div></md-option><!----><md-option ng-repeat="loyaltyCard in ::$ctrl.loyaltyCards" ng-value="::loyaltyCard.value" tabindex="0" class="md-ink-ripple" role="option" aria-selected="false" id="select_option_2171" aria-checked="true" value="DY"><div class="md-text">
            Norwegian Reward (DY)
          </div></md-option><!----><md-option ng-repeat="loyaltyCard in ::$ctrl.loyaltyCards" ng-value="::loyaltyCard.value" tabindex="0" class="md-ink-ripple" role="option" aria-selected="false" id="select_option_2172" aria-checked="true" value="D8"><div class="md-text">
            Norwegian Reward (D8)
          </div></md-option><!----><md-option ng-repeat="loyaltyCard in ::$ctrl.loyaltyCards" ng-value="::loyaltyCard.value" tabindex="0" class="md-ink-ripple" role="option" aria-selected="false" id="select_option_2173" aria-checked="true" value="EK"><div class="md-text">
            Skywards (EK)
          </div></md-option><!----><md-option ng-repeat="loyaltyCard in ::$ctrl.loyaltyCards" ng-value="::loyaltyCard.value" tabindex="0" class="md-ink-ripple" role="option" aria-selected="false" id="select_option_2174" aria-checked="true" value="TP"><div class="md-text">
            Air Portugal (TAP)
          </div></md-option><!---->
        </md-content></md-select-menu></div>

element( by.css('.md-select-menu-container md-option[value="blue_biz"]')).click();

4 个答案:

答案 0 :(得分:1)

您可以尝试使用by.cssContainingText(),如下所示:

element(by.css('.md-select-menu-container md-select-menu md-content md-option')).element(by.cssContainingText('.md-text', 'Blue Biz (AF/KL)')).click();

此代码选择值为“ Blue Biz(AF / KL)”的选项

如果以上代码不起作用,请尝试以下代码:

function clickOption(searchText) {
    let wrapper = element(by.css('.md-select-menu-container'));
    let selectMenu = wrapper.element(by.css('md-select-menu'));
    let mdContent = selectMenu.element(by.css('md-content'));
    let options = mdContent.all(by.css('md-option'));
    return options.filter(el => {
      return el.element(by.css('.md-text')).getText().then(text => text.trim() === searchText);
    }).first().click();
  }

// ...
clickOption('Blue Biz (AF/KL)');

在此代码中,所有选择器都是分开并链接的。这样,您可以确定实际上找不到哪个选择器。也许已经是第一个了。除此之外,该函数实际上返回匹配的md-option元素。也许不可能click()底层div

答案 1 :(得分:0)

这将在下拉列表中选择文本“ SAS信用(SK)”,您可以根据您的 DOM 替换值:

element(by.id("select_container_2163")).element(by.css("[value='SK']")).click();

答案 2 :(得分:0)

此下拉列表是使用divs and spans进行的,因此 select 类在这种情况下将不起作用。

您将必须使用下拉列表存储存在的每个元素,然后可以通过必需的文本选择它。像

 List<WebElement> allValues =   driver.findElements(By.cssSelector("div.md-text"));
     for(WebElement values : allValues) {
         if(values.getText().trim().contains("SAS Credit (SK)")) {
             values.click();
         }   
     }

答案 3 :(得分:0)

最后我可以单击下拉))

   var dropdown = element(by.model('card.type'));
      var dropdownItems = element.all(by.repeater("loyaltyCard in ::$ctrl.loyaltyCards"));

      dropdown.click().then(() => {
          dropdownItems.get(2).click();
      });