Robot FrameWork之元素定位xpath


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()="新闻")]
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值