最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Selecting second anchor element within lists using protractor - Stack Overflow

programmeradmin4浏览0评论

I have a list of anchor tags and have to test clicking the 2nd tag in the list.

<ul id="shortcuts">
    <li><a ui-sref="app.journeyExplorer" href="#/journey-explorer/"><span class="ng-binding">1</span></a></li>
    <li><a ui-sref="app.userGroupManager" href="#/user-group-manager"><span class="ng-binding">2</span></a></li>
</ul>

After much investigation and testing reached at the conclusion that the correct statement should be:

element(By.id('shortcuts')).element(By.tagName('a')).get(1).click();

But it shows undefined. If I use get() it doesn't work. without get it clicks the 1st anchor tag in the list with a warning: 'More than one anchor tag found, in such case the 1st one is selected'

Can someone please help out with this ? Thanks.

I have a list of anchor tags and have to test clicking the 2nd tag in the list.

<ul id="shortcuts">
    <li><a ui-sref="app.journeyExplorer" href="#/journey-explorer/"><span class="ng-binding">1</span></a></li>
    <li><a ui-sref="app.userGroupManager" href="#/user-group-manager"><span class="ng-binding">2</span></a></li>
</ul>

After much investigation and testing reached at the conclusion that the correct statement should be:

element(By.id('shortcuts')).element(By.tagName('a')).get(1).click();

But it shows undefined. If I use get() it doesn't work. without get it clicks the 1st anchor tag in the list with a warning: 'More than one anchor tag found, in such case the 1st one is selected'

Can someone please help out with this ? Thanks.

Share Improve this question edited Nov 10, 2016 at 10:50 konqi 5,2273 gold badges37 silver badges53 bronze badges asked Jul 27, 2015 at 8:24 Prateek ChoudhuryPrateek Choudhury 3023 gold badges8 silver badges21 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 13

You might want to try selector below (note all() instead of second element() to match all anchors (so that .get() that es next makes any sense.

element(By.id('shortcuts')).all(By.tagName('a')).get(1).click();

or via .css

element(By.css('#shortcuts a:nth-child(1)').click();
发布评论

评论列表(0)

  1. 暂无评论