【问题标题】:Selecting an element from a drop-down with Protractor使用量角器从下拉列表中选择元素
【发布时间】:2017-10-03 14:55:44
【问题描述】:

我在使用 Angular 构建的应用程序的一页上使用常规 selenium webdriver 时遇到严重问题。我在 pom.xml 中添加了 ngwebdriver 的依赖项,到目前为止我已经解决了一些问题。只有下拉,我还没有弄清楚如何做到这一点。这是我正在谈论的下拉菜单:

 <d3-data-list _nghost-c8="" ng-reflect-data-source="[object Object]" ng-
reflect-html-id="documents_glossary_entityPicke" ng-reflect-allow-blank-
option="true" ng-reflect-blank-option-text="All entities" ng-reflect-default-
caption="Choose an entity"><div _ngcontent-c8="" class="dropdown d3-data-
list" id="documents_glossary_entityPicker">
  <button _ngcontent-c8="" aria-expanded="true" aria-haspopup="true" class="btn btn-default dropdown-toggle" data-toggle="dropdown" type="button" id="documents_glossary_entityPicker_button">
All entities
<span _ngcontent-c8="" class="caret" 
id="documents_glossary_entityPicker_caret"></span>
  </button>
  <ul _ngcontent-c8="" aria-labelledby="dropdownMenu1" class="dropdown-menu" 
ng-reflect-infinite-scroll-handle-in-progress="false" 
id="documents_glossary_entityPicker_dropdown">
    <!--bindings={
      "ng-reflect-ng-if": "true"
}--><li _ngcontent-c8="">
      <a _ngcontent-c8="" 
id="documents_glossary_entityPicker_blank_option">All entities</a>
</li>

那么下拉菜单中的选项是这样的:

</li><li _ngcontent-c8="">
  <a _ngcontent-c8="" id="documents_glossary_entityPicker_option_2">Entity three</a>
</li>

问题是我的下拉列表中元素的顺序没有保持,因为如果在另一个页面上编辑元素(实体),那么它将位于列表的底部。所以我永远无法真正知道我的元素在下拉列表中的确切位置。

有没有像browser.findElement(ByAngular.buttonText("text")) 这样的东西可以用于我的情况?谢谢。

【问题讨论】:

    标签: java angularjs selenium protractor


    【解决方案1】:

    我猜你正在寻找filter().reduce() 的可能性。

    对于.reduce(),请检查the example here

    对于.filter(),您的代码应如下所示:

    element.all(by.css('ul li a')).filter(function (elm) {
        return elm.getAttribute('value').then(function (text) {
            return text === "TheValueYouLookFor";
        });
    }).first().click();
    

    我自己没有测试过这个.filter,但它从Protractor documentation 非常简单。

    【讨论】:

      猜你喜欢
      • 2020-06-04
      • 1970-01-01
      • 1970-01-01
      • 2015-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-30
      • 1970-01-01
      相关资源
      最近更新 更多