【问题标题】:Looking to get index of first matched element within array寻找数组中第一个匹配元素的索引
【发布时间】:2015-02-27 08:24:44
【问题描述】:

我希望在转发器中获取匹配元素的索引位置,并想知道如何找到。

以下书籍列表

element.all(by.repeater('books'))

在这些行中,一些行将具有下面的元素

element(by.css('[ng-click="download"]')

我想做以下事情

  1. 找到下载按钮的元素(超过1个)
  2. 获取第一个下载按钮的索引位置(这样我以后可以使用 .get(3) 为特定行做其他事情)

所以这就是我尝试过的。

element.all(by.css('some-css')).first().element(by.tagName('tag-within-css')).get(index);
element.all(by.css('some-css')).get(index).element(by.tagName('tag-within-css'));

Finding Sub-Elements - Protractor locators guide

一些错误,例如 index not definedno method of 'get'

谢谢。

【问题讨论】:

    标签: javascript angularjs protractor angularjs-e2e


    【解决方案1】:

    我认为您不需要在中继器内使用元素位置/索引进行操作。相反,通过链接elementelement.all,找到下载按钮并使用上下文/元素特定元素搜索对当前行进行操作:

    var books = element.all(by.repeater('books'));
    
    var downloadButtons = books.all(by.css('[ng-click="download"]'));
    var firstDownloadButton = downloadButtons.first();
    
    // here is how you can, for example, get the next div element to the download button  
    var nextElement = firstDownloadButton.element(by.xpath('following-sibling::div'));
    

    【讨论】:

    • 感谢@alecxe 的回复。我想获取索引以检查该行的复选框是选中还是未选中。顺便说一句,这些链接是否适用于数组中的数组?日志显示所有来自 downloadButtons 的内容都未定义。
    • 好的@alecxe,我做了一些小的调整,没有看到错误,但它没有点击第一个下载按钮。我删除了 downloadButtons 并更新为 var firstDownloadButton = bookingRequest.first().all(by.css('[ng-click="downloadAttachment(item)"]'));
    • @user2388556 看起来你已经解决了,对吧?感谢您的编辑!
    • 是的@alecxe。在再次查看文档并意识到我需要从 downloadButtons 中删除“元素”后,它会在第一个元素上单击。再次感谢您的回答。
    猜你喜欢
    • 1970-01-01
    • 2015-05-23
    • 2013-01-10
    • 1970-01-01
    • 2018-12-29
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多