【定位】文本定位方式

(//*[contains(text(),"南昌艾泰科技有限公司")])[2]

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

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])

原文地址:https://www.cnblogs.com/bashaowei/p/9324011.html