使用Selenium必会之技能,xpath 定位元素

《跟着日邢一珊,学习自动化测试》-----第四讲20170824

WEB自动化测试Selenium 的使用 (四)  XPATH元素定位。

作者:日邢一珊 

自动化测试脚本分享群:654395302  、 567415093

打赏,就是鼓励我写作的动力。 2.88 3.88 6.88 12.88 都是不错的数字呢~

第一种方法:通过绝对路径做定位(相信大家不会使用这种方式)

By.xpath("html/body/div/form/input")

第二种方法:通过相对路径做定位

两个斜杠代表相对路径

By.xpath("//input//div")

第三种方法:通过元素索引定位

By.xpath("//input[4]")

第四种方法:使用xpath+节点属性定位(结合第2、第3中方法可以使用)

By.xpath("//input[@id='kw1']")
By.xpath("//input[@type='name' and @name='kw1']")

第五种方法:使用部分属性值匹配(最强大的方法)

By.xpath("//input[start-with(@id,'nice')]")
By.xpath("//input[ends-with(@id,'很漂亮')]")
By.xpath("//input[contains(@id,'那么美')]")

第六种方法:使用前集中方法的组合

By.xpath("//input[@id='kw1']//input[start-with(@id,'nice']/div[1]/form[3])

打赏,就是鼓励我写作的动力。 2.88 3.88 6.88 12.88 都是不错的数字呢~

 以上,谢谢阅读。

原文地址:https://www.cnblogs.com/xingyishan/p/7425095.html