【发布时间】:2014-09-26 00:07:09
【问题描述】:
我已阅读此question and its answer,并希望更进一步。
我想使用CasperJS.click(selector) 函数点击匹配选择器的多个链接。请注意,链接没有明显的href 标签。
考虑以下标记:
HTML:
<div>
<h1 class='myLink'>Cocacola</h1>
<div>
<div>
<h1 class='myLink'>Sprite</h1>
</div>
</div>
</div>
我在上面提到的答案建议删除链接,以便我们可以使用casper.exists 等单击剩余元素。如果我不想操作页面怎么办?
出于我无法想象的原因,使用:
document.querySelector("div .myLink:nth-of-type(1)");
捕获第一个h1,可口可乐。但是:
document.querySelector("div .myLink:nth-of-type(2)");
返回null。
有什么想法吗?非常感谢!
【问题讨论】:
-
如果您不希望加载新页面,则可以使用this。
-
nth-of-type仅对元素类型起作用,在本例中为h1。它不会考虑.myLink类。索引仅对同一父级下的元素有意义。
标签: javascript css-selectors casperjs selectors-api