selenium元素定位之多个元素中选择其中的一个

本文转自:http://t.zoukankan.com/yanzhuping-p-14334447.html

 

1、xpath匹配到多个元素如何选择指定的元素定位

(1)xpath匹配到多个元素,选择指定的元素,通过末尾的数字选择具体的元素

(//span[@id=‘example’])[2]

(2)xpath定位到统计标签的最后一个

//span[@id='example']/a[last()]

(3)xpath定位同级标签的倒数第二个

//span[@id='example']/a[last()-1]

2、css选择器:nth-child(),选择的是父元素的第n个元素

(1)选择列表中的偶数标签 :nth-child(2n)

(2)选择列表中的奇数标签  :nth-child(2n-1)

(3)选择从第6个开始,直到最后   :nth-child(n+6)

(4)选择第1个到第6个  :nth-child(-n+6)

(5)选择第6个到第9个  :nth-child(n+6):nth-child(-m+9)

(6)选择倒数第N个  :nth-last-child(N)   注意:N可以使数字、关键词或者公式

(7)选择特定类型的第n个子元素的每个元素  :nth-of-type(N)  注意:N可以使数字、关键词或者公式

(8)选择除第一个以外的所有   :nth(:first-child)

(9)选择除最后一个元素以外的所有   :nth(:last-child)

 

posted @ 2022-11-09 14:25  八戒不爱吃西瓜  阅读(1032)  评论(0编辑  收藏  举报