量角器:如何达到第二个href?

时间:2014-03-31 10:43:03

标签: jquery href

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

我可以用$('.divName a').click();到达第一个href,但我怎样才能到达第二个?

4 个答案:

答案 0 :(得分:3)

通过在all上使用。element方法,您可以返回一系列项目,然后您可以访问这些项目。这样就无需添加类就可以通过测试。例如:

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

在量角器测试中:

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');

答案 1 :(得分:1)

可能会添加一些课程帮助你

<div class="divName">
   <a href="some url" class="first">some text</a>
   <a href="some url" class="second">another text</a>
</div> 
// jquery
$('.first').click();
$('.second').click();

或者如果您不想使用课程,请使用jquery .eq()

$('.divName a').eq(1).click();  // trigger click event for second link

:eq()选择器

$('.divName a:eq(1)').click();  // trigger click event for second link 

答案 2 :(得分:0)

你可以&#34;达到&#34;第一个因为select $('.divName a')将返回一个对象数组,每个对象都是href

您可以使用[.next()][1],但这有点不稳定且不可靠。我将ID应用于href,以便您可以永久可靠地绑定它们。

答案 3 :(得分:0)

或迭代:

<script type="text/javascript">
    $('.divName a').each(function (index, element) {
        alert($(element).attr('href'));
       // element.click();
    });
</script>