Select first visible element in protractor

前端 未结 1 724
清歌不尽
清歌不尽 2021-02-19 21:30

I\'m writing protractor tests and like it, although sometimes seem to get caught up on something that seems as if it should be simple. For example, I want to loop through all o

1条回答
  •  难免孤独
    2021-02-19 22:08

    els is defined. What's not defined is x. The easy way to do it is:

    var nominateButtons = element.all(by.buttonText('Nominate'));
    var displayedButtons = nominateButtons.filter(function(elem) {
       return elem.isDisplayed(); 
    });
    displayedButtons.first().click();
    

    or

    element.all(by.buttonText('Nominate')).
      filter(function(elem) {
        return elem.isDisplayed(); 
      }).
      first().
      click();
    

    EDIT, by the way, you shouldn't rely on this behavior (click first button of text 'Nominate') because it will lead to issues when you change your app. See if you can either select by ID, or select a more specific 'Nominate' like element(by.css('the section the nominate is under')).element(by.buttonText('Nominate'));

    EDIT again: see Using protractor with loops for explanation

    0 讨论(0)
提交回复
热议问题