selenium-xpath选择操作web元素

1.根据属性选择

(1)//*[@style]选择所有具有style属性的元素   #css写法  *[style]

(2)//p[@spec='len2‘选择所有具有spec值为len2的元素   #css写法p[spec=len2]

2.根据id选择

id,class也是属性

//div[@id="food"]  #css写法#food

//div[@class="cheese"]  #css写法.cheese

3.子元素选择

选择属于其父元素的第n个某个类型的子元素

//p[2]等价于css写法p:nth-of-type(2)

//*[@id-"food"]/p[1]等价于css写法#food>p:nth-of-type(1)

//*[2]等价于//*[position()-2]等价于*:nth-child(2)

//*[@id-"food"]/*[position()-3]

支持其他的比较操作符

//*[@id-"food"]/*[position()<3]

//*[id-"food"]/*[position()<-3]

选择属于其父元素的倒数第n个子元素

//*[@id-"food"]/*[last()-1]等价于

//*[@id-”food"]/*[position()-last()-1]

多选://*[@id="food"]/*[position()>last()-3]

4.组选择

多个表达式在一起

css用,隔开

比如p,button

xpath用|隔开

//p|//button

5.其他选择语法

相邻兄弟选择器

following-sibling

preceding-sibling

xpath擅长的

..上层元素

原文地址:https://www.cnblogs.com/iamshasha/p/11112610.html