jquery - protractor : How to reach the second href?

标签 jquery href

<div class="divName">
  ...
  <a href="some url">some text</a>
  <a href="some url">another text</a>
  ...
</div>

我可以使用 $('.divName a').click(); 到达第一个 href,但是如何到达第二个 href?

最佳答案

通过在 element 上使用 .all 方法,您可以返回一个项目数组,然后您可以访问该数组。这避免了仅仅为了通过测试而添加类的需要。例如:

<div class="divName">
  ...
  <a href="some url">some text</a>
  <a href="some url">another text</a>
  ...
</div>

在 protractor 测试中:

var urls = element.all(by.css('.divName a'));

expect(urls.count()).toBe(2); //passes test
urls.get(1).click().then(function() { ... };
// access an item in array with the .get(index) method, 
// then follow the link with click() should you wish

还有一个快捷方法:

var urls = $$('.divName a');

关于jquery - protractor : How to reach the second href?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22759509/

相关文章:

javascript - HTML TAG <A> 使用 href 和 onclick 传递变量

jquery - 想要阻止页面重新加载/离开直到 Ajax 请求完成

javascript - 在 beforeunloadhow 之前没有触发 change 事件

jQuery,使用 <select> 的最佳方式

javascript - 滚动固定顶部按钮 | CSS 和 jQuery

javascript - 在新窗口中打开链接,而不是在新选项卡中

javascript按钮点击功能不起作用

jquery - Href 页面导航不适用于单选(输入)按钮

javascript - 将字符串转换为 HTML - 字符串到 "a href"元素

javascript - 有没有更好的方法来防止单击 href 时整个页面滚动?