javascript 使用量角器选择列表中的第二个锚元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31648203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:08:10  来源:igfitidea点击:

Selecting second anchor element within lists using protractor

javascriptangularjsseleniumselenium-webdriverprotractor

提问by Prateek Choudhury

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'

但它显示未定义。如果我使用 get() 它不起作用。没有得到它点击列表中的第一个锚标记并显示警告:“找到多个锚标记,在这种情况下,第一个被选中”

Can someone please help out with this ? Thanks.

有人可以帮忙吗?谢谢。

回答by WTK

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.

您可能想尝试下面的选择器(注意all()而不是第二个element()匹配所有锚点(这样.get()接下来的选择才有意义。

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

or via .css

或通过 .css

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