Selecting second anchor element within lists using protractor

时光毁灭记忆、已成空白 提交于 2019-12-22 08:29:50

问题


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.


回答1:


You might want to try selector below (note all() instead of second element() to match all anchors (so that .get() that comes 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();


来源:https://stackoverflow.com/questions/31648203/selecting-second-anchor-element-within-lists-using-protractor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!