selenium使用Xpath定位之完整篇
其中有一片文章提到了xpath元素定位,但是该文章中有些并不能适应一些特殊与个性化的场景。在文本中提供xpath元素的定位终极篇,你一定能在这里找到你需要的解决办法。
第一种方法:通过绝对路径做定位(相信大家不会使用这种方式)
By.xpath("html/body/div/form/input")
第二种方法:通过相对路径定位(大家最常使用的方式)
By.xpath("//input[4]")
第三种方法:通过元素索引定位
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,那么美)]")
第6种方法:使用xpath轴(未曾使用)
希望,以上这些方法,能够帮助到你。
上一篇:
Python 安装包管理工具 pip