selenium使用Xpath定位之完整篇

其中有一片文章提到了xpath元素定位,但是该文章中有些并不能适应一些特殊与个性化的场景。在文本中提供xpath元素的定位终极篇,你一定能在这里找到你需要的解决办法。

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

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

第二种方法:通过相对路径定位(大家最常使用的方式)

By.xpath("//input")

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

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

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

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

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

  1.  
    By.xpath("//input[start-with(@id,'nice')
  2.  
    By.xpath("//input[ends-with(@id,'很漂亮')
  3.  
    By.xpath("//input[contains(@id,'那么美')]")
原文地址:https://www.cnblogs.com/peak911/p/9516680.html