Robot FrameWork之元素定位xpath

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/chuhe163/article/details/102584388


1、绝对路径

  • 尽量不使用,除非已经使用了所有方式仍然无法定位
xpath=/html/body/div[1]/div[1]/div/div[1]/div/form/span[1]/input

2、相对路径

2.1、id

  • @后可跟任意属性
  • id是唯一的
xpath=//input[@id='kw']

2.2、name

  • @后可跟任意属性
  • name并不一定唯一,默认定位第一个
xpath=//input[@name='wd']

2.3、通过父节点元素的唯一属性定位

xpath=//form[@id='form']/span/input
xpath=//span[@class="bg s_ipt_wr quickdelete-wrap"]/input[@id="kw"]

2.4、xpath做布尔逻辑运算

xpath=//input[@id="kw" or @name="wd"]
  • 对条件过滤
xpath=//input[@id="kw" and @name="wd"]

2.5、模糊定位

2.51、contains 方法

xpath=//input[contains(@name,"wd")]

2.52、 starts-with(以XX开头)

xpath=//a[starts-with(@href,'http://news.baidu')]

2.53、 ends-with(以XX结尾)

  • 与starts-with用法一致
  • ends-with是xpath 2.0的部分,Chrome通常只支持xpath1.0,可以用下面的方法替代
xpath=//a[substring(@href,string-length(@href)-string-length("news.baidu.com")+1)="news.baidu.com"]

2.54、text()方法

  • 包含指定文字的链接
xpath=//a[contains(text(),"新闻")]
  • 全部指定文字的链接
xpath=//*[(text()="新闻")]

猜你喜欢

转载自blog.csdn.net/chuhe163/article/details/102584388