Xpath元素定位:
1. Xpath select node - path matching:
- / absolute path;
- // relative path;
- . select current node;
- .. select the parent node of current node;
- @ 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::* //获取元素紧邻的前面所有兄弟节点元素
- parent eg. //div[@id=’xxx’]/parent::*
- children eg.//div[@id='xxx']/child::*
- following::* 元素的后序的所有元素
- preceding::* 元素的所有前年的元素
- sibling
- descendant eg. //table[contains(@class,'orderHistoryBox') and descendant::dd[text()='11132789']] //所有后代元素
- ancestor eg. //dd[text()='11132789']//ancestor::table[contains(@class,'orderHistoryBox')] //祖先元素table,并且class 包含 orderHistoryBox
- ancestor-or-self eg. //input[@type='hidden' and @name="tn"]/ancestor-or-self::* //返回自身及祖先节点元素
- descendant-or-self eg. //form[@id='form']/descendant-or-self::* //返回自身及后代节点元素
3. Xpath – predicate []
- //ul/li[1]
- //ul/li[last()]
- //ul/li[last()-1]
- //ul/li[position()<3]
- /a[@href]
- /a[@href=’xxx’]
- //input[contains(@id, ‘xxxx’)]
- //input[@type=’name’ and @name=’xxx’]
- //a[contains(text(), ’textxxx’)]
- //table[@id='wizards-ivrKeyPressAssignment-extSelectorForm-mailbox-field']/tbody/tr[not(contains(@class,'x-hidden'))][5]
- //*[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
- last() eg. //form[@id='form']/input[last()]
- position() eg. //form[@id='form']/input[position()<3]
- starts-with() eg. //form[@id='form']/input[starts-with(@name,'rs')]
- ends-with() eg. //article[ends-with(@id,'logo')] //?未调试通过