我正在尝试使用 css 选择器来选择和项目,但仅适用于 xpath。这个 xpath 到 css 的等价物是什么。
driver.findElement(By.xpath("(//img[@alt='Authorise this row'])[2].click();
如果我使用 css img[alt='Authorise this row']
,我会通过 firefinder 获得很多结果。有没有像 xpath 这样的方法,您可以在其中获得结果的特定索引
CSS3 支持 :nth-child 选择器。例如,以下 CSS 仅针对第二个图像:
img:nth-child(2) {...}
:nth-child 的参数可以是索引、关键字(例如奇数、偶数)或公式。上面的选择器选择了他们父母的所有第二张图片孩子。您可以通过以下方式将其定位更多:
#container img:nth-child(2)
参考::nth-child
或者,您可以使用 jQuery 选择一组元素,然后通过索引引用其中一个。像这样:
var secondImage = $('img')[2];