nth类型css选择器 [英] nth-of-type css selector
问题描述
以下是我的HTML页面的代码段:
Below is a snippet of my HTML page:
<td id="Platinum" align="center" width="16%">
<div></div>
<a>link1</a>
</td>
<td></td>
<td id="Platinum" align="center" width="16%">
<div></div>
<a>link2</a>
</td>
<td id="Platinum" align="center" width="16%">
<div></div>
<a>link3</a>
</td>
在这里,我要选择id ='platinum'的第三个td元素
Here I want to select third td element with id='platinum'
我尝试的是 driver.findElement(By.cssSelector(#platinum:nth-of-type(3)));
。
和
driver.findElement(By.cssSelector(td [id = 'platinum']:nth-of-type(3)));
td with id ='platinum'
推荐答案
code> td 没有ID,这会干扰:nth-of-type()
计数。选择器意味着 td
元素,而不是nth td
铂'。因此,具有该ID的第三个 td
实际上是整个行中的第四个 td
。
There is an empty td
that doesn't have the ID, which is interfering with the :nth-of-type()
count. The selector means "nth td
element", not "nth td
element with id='platinum'
". The third td
with that ID is therefore actually the fourth td
in the entire row.
使用CSS选择器不能选择第三个具有某个ID的 td
元素。您应该使用XPath:
It's not possible to select the third td
element with a certain ID using a CSS selector. You should use XPath instead:
driver.findElement(By.xpath("//td[@id='Platinum'][3]"));
这篇关于nth类型css选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!