selenium中Xpath定位元素

    xiaoxiao2022-07-12  158

    第一种绝对定位

    By.Xpath("/html/body/div/from/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])

     

     

    最新回复(0)