美文网首页
selenium 定位之xpath方法详解

selenium 定位之xpath方法详解

作者: TungChiu | 来源:发表于2017-12-16 11:55 被阅读0次

声明:转摘自

https://mp.weixin.qq.com/s?__biz=MzA4MTcyOTEwMw==&mid=2650610448&idx=3&sn=bf50f9650f3bb085ba43c0730baec3ee&scene=0#wechat_redirect

在文本中提供xpath元素的定位终极篇,你一定能在这里找到你需要的解决办法。

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

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

By.xpath("//input")

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

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

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

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

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

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

查找id属性开始位置包含 '很漂亮' 关键字的元素

By.xpath("//input[start-with(@id,'nice')]")

查找id属性结尾位置包含 '很漂亮' 关键字的元素

By.xpath("//input[ends-with(@id,'很漂亮')]")

查找id属性包含 '那么美' 关键字的元素

By.xpath("//input[contains(@id,'那么美')]")

相关文章

网友评论

      本文标题:selenium 定位之xpath方法详解

      本文链接:https://www.haomeiwen.com/subject/qnrswxtx.html