自动化元素定位 - Xpath

本文深入解析XPath元素定位技术,涵盖绝对路径、相对路径、属性选择、节点关系等核心概念,如父节点、子节点、兄弟节点、后代节点的选取方法,并介绍如何使用谓词和变量进行精确匹配,以及XPath函数的运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Xpath元素定位:

1. Xpath select node - path matching:

  1. /        absolute path;
  2. //      relative path;
  3. .        select current node;
  4. ..       select the parent node of current node;
  5. @     select attribute                           eg.     //div[@id='username']

 

2. Xpath node - relationship:

                                        following-sibling::*      //input[@type='hidden' and @name="rsv_bp"]/following-sibling::*      //获取元素紧邻的后序所有兄弟节点元素

                  preceding-sibing::*      //input[@type='hidden' and @name="tn"]/preceding-sibling::*         //获取元素紧邻的前面所有兄弟节点元素

 

  1. parent     eg. //div[@id=’xxx’]/parent::*
  2. children   eg.//div[@id='xxx']/child::*
  3. following::*    元素的后序的所有元素
  4. preceding::* 元素的所有前年的元素
  5. sibling
  6. descendant    eg. //table[contains(@class,'orderHistoryBox') and descendant::dd[text()='11132789']]        //所有后代元素
  7. ancestor         eg. //dd[text()='11132789']//ancestor::table[contains(@class,'orderHistoryBox')]       //祖先元素table,并且class 包含 orderHistoryBox
  8. ancestor-or-self      eg. //input[@type='hidden' and @name="tn"]/ancestor-or-self::*    //返回自身及祖先节点元素
  9. descendant-or-self     eg. //form[@id='form']/descendant-or-self::*    //返回自身及后代节点元素

3. Xpath – predicate  []

  1. //ul/li[1]
  2. //ul/li[last()]
  3. //ul/li[last()-1]
  4. //ul/li[position()<3]
  5. /a[@href]
  6. /a[@href=’xxx’]
  7. //input[contains(@id, ‘xxxx’)]
  8. //input[@type=’name’ and @name=’xxx’]
  9. //a[contains(text(), ’textxxx’)]
  10. //table[@id='wizards-ivrKeyPressAssignment-extSelectorForm-mailbox-field']/tbody/tr[not(contains(@class,'x-hidden'))][5]
  11. //*[contains(@id,'userCallForwarding/rules-hoursSelector-tabBar-root-item') and contains(@class,'textTabButtonSelected')]

4. Xpath – variable

eg. extensionDescription.xpath=//tbody[@id='system-extensions-usersGrid-tbody']//div[@class='system-extensions-title' and text()='$VALUE']/../../following-sibling::*[1]

5. Xpath - Funcation

  1. last()     eg. //form[@id='form']/input[last()]
  2. position()    eg. //form[@id='form']/input[position()<3]
  3. starts-with()    eg.  //form[@id='form']/input[starts-with(@name,'rs')]
  4. ends-with()         eg. //article[ends-with(@id,'logo')]     //?未调试通过

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值