我如何从链接集合中获取一些特定的链接 [英] How can I get some particular link form the links collection
问题描述
我使用的是watir-webdriver,其中几行显示的代码相同,如下所示:
I'm using watir-webdriver have several lines of the same code shown below:
...
<p class="schedule-text">
by
<a href="http://www.somesite.com">MarketingClub</a>
in
<a href="http://www.somesite2.com">Marketing</a>
</p>
我需要获取p标签中包含的第一个链接以及p标签中包含的第二个链接 因此,使用页面对象我添加了以下代码:
I need to get the first links included in p tag and the second links included in p tag so using page object I've added the following code:
links(:followees_category, :css => "#home-followees li.animate-in ul li[data-show-id] p.schedule-text a")
...
followees_category_elements.attribute("href")
最后一行将给我两个链接: http://www.somesite2.com , http://www.somesite2.com 不幸的是,我不能在css:last/:first等中指出.
the last row will give me both links : http://www.somesite2.com, http://www.somesite2.com Unfortunately, I can't indicate in css :last/:first etc.
第二个链接可以通过将css更改为:
the second link can be gotten by changing css to :
#home-followees li.animate-in ul li[data-show-id] p.schedule-text a + a
但是我怎样才能仅从这些块中获得第一个链接? 当然,我可以同时获得两个链接并使用它们,但是也许有替代的解决方案?
but how can I get just the first links from such blocks? Of course, I can get both links and work with every 2nd, but maybe there is an alternative solution?
推荐答案
您可以使用CSS nth-of-type
伪类基于元素的索引(或相对位置)获取元素.
You can use the css nth-of-type
pseudo class to get elements based on their index (or relative position).
例如,a:nth-of-type(1)
可用于返回作为其父代第一行的所有链接:
For example, a:nth-of-type(1)
can be used to return all links that are the first line of their parent:
links(:followees_category, :css => "p.schedule-text a:nth-of-type(1)")
对于第二个链接,您可以执行a:nth-of-type(2)
.请注意,它是基于1的索引.
For the second links, you can do a:nth-of-type(2)
. Note that it is 1-based index.
links(:followees_category, :css => "p.schedule-text a:nth-of-type(2)")
对于第一个链接,您还可以执行a:first-of-type
:
For the first link, you can also do a:first-of-type
:
links(:followees_category, :css => "p.schedule-text a:first-of-type")
如果第二个链接始终是最后一个链接,则也可以执行a:last-of-type
:
If the second link is always the last link, you can also do a:last-of-type
:
links(:followees_category, :css => "p.schedule-text a:last-of-type")
这篇关于我如何从链接集合中获取一些特定的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!