xpath知识参考
[参考文章2](https://www.cnblogs.com/dong-dong-dong/p/9579038.html)
常规:
1、文本全部匹配://标签名[text()=值]
xpath=//span[text()='提交']
2、文本部分匹配://标签名[contains(text(),值)]
xpath=//span[contains(text(),'提交')]
3、属性全部匹配://标签名[@属性名=值]
xpath=//span[@class="resourceGroupText ng-binding"] --class属性寻找元素
xpath=//button[@class='choose btn btn-primary btn-sm'] --button属性
xpath=//label[@for='fileType']--label属性
4、属性部分匹配://标签名[contains(@属性名,值)]
xpath=//span[contains(@class,'resourceGroupText')] --根据部分class属性寻找元素
特殊:
1、XPath 轴(Axes):
ancestor:祖先节点,包括父节点
parent:父节点
preceding:当前节点标签之前的所有节点
preceding-sibling:当前节点前的所有兄弟节点(同级)
following:当前节点标签之后的所有节点
following-sibling:当前节点标签之后的所有兄弟节点(同级)
xpath=//li//span[text()="Stop"]/parent::*/parent::*//span[text()='Start'] -----------根据某唯一元素,反向找到其父元素,再重新向下找
xpath=//td[text()='${parameter}']/following-sibling::td[4]/div/a[1] -----------根据某唯一元素,找到其后的兄弟元素(${parameter}是变量)
xpath=//input[@id='fileUrls']/preceding-sibling::a[1] -----------根据某唯一元素,找到其前面的兄弟元素
//span[contains(text(),'${parameter}')]/ancestor::td/following-sibling::td//span[contains(text(),'解除')]-----根据某唯一元素,找到其祖先节点td,再找到td之后的兄弟节点的的下级元素(td后面的//,选择属于 td 元素的后代的所有span[contains(text(),'解除')] 元素,而不管它们位于 td之下的什么位置)
网友评论