1. 绝对定位:

driver.find_element_by_xpath("/html/body/div[x]/form/input") x 代表第x个 div标签,注意,索引从1开始而不是0

2.相对路径:

driver.find_element_by_xpath("//input[x]") 定位第x个input标签,[x]可以省略,默认为第一个

3.标签属性定位:

driver.find_element_by_xpath("//a[@href='/industryMall/hall/industryIndex.ht']")
driver.find_element_by_xpath("//input[@value='确定']")
driver.find_element_by_xpath("//div[@class = 'submit']/input")
driver.find_element_by_xpath("//input[@type='name' and @name='kw1']")

driver.find_element_by_xpath("//标签[contains(text(),'文本值')]")

driver.find_element_by_xpath("//input[contains(text(),'型号')]")

driver.find_element_by_xpath(“//a[contains(@href, ‘logout’)]”)

a.starts-with      例子: input[starts-with(@id,'ctrl')]                  解析:匹配以ctrl开始的属性值

b.ends-with        例子:input[ends-with(@id,'_userName')]     解析:匹配以userName结尾的属性值   好像不行,经常报错,不推荐

c.contains()        例子:Input[contains(@id,'userName')]         解析:匹配含有userName属性值

driver.find_element_by_xpath("//input[@id='kw1']//input[start-with(@id,'nice']/div[1]/form[3])  如果上面的单一方法不能完成定位,也可以采取组合式定位

4.Xpath轴方式定位元素 

1、child  选取当前节点的所有子元素

driver.find_element_by_xpath("//div[@id='B']/child::div")

2、parent  选取当前节点的父节点

driver.find_element_by_xpath("//div[@id='C']/parent::*/parent::div")

driver.find_element_by_xpath("//div[@id='C']/..")   后面两点也代表上一级的父节点

3、descendant选取当前节点的所有后代元素(子、孙等)

"//form[@id='form']/descendant::input[@id='su']"

4、ancestor  选取当前节点的所有先辈(父、祖父等)

//*[@id='%s']/ancestor::td/preceding-sibling::td[1]

5、preceding-sibling选取当前节点之前的所有同级节点

driver.find_element_by_xpath("//div[@id='D']/preceding-sibling::div[1]").text

6、following-sibling选取当前节点之后的所有同级节点

driver.find_element_by_xpath("//td[contains(text(),’17051915200001’)]/following-sibling::td[8]/a[@class='link']")

7、preceding   选取文档中当前节点的开始标签之前的所有节点

//p[text()='出售方信息']/preceding::div[contains(text(),'拟定网签价')]

8、following   选取文档中当前节点的结束标签之后的所有节点

//td[@width='50%']/following::div[contains(text(),'行政区域')]

最新文章

  1. Python基础篇【第7篇】: 面向对象(1)
  2. 如何让vim编辑器永久显示行号
  3. CentOS升级Python 2.6到2.7
  4. 树的统计Count---树链剖分
  5. android用欢迎界面加载运行环境
  6. BZOJ_1029_[JSOI2007]_建筑抢修_(贪心+优先队列)
  7. 模版引擎(NVelocity)开发
  8. iOS常用动画-b
  9. Linux&shell之高级Shell脚本编程-创建菜单
  10. java中三种常见内存溢出错误的处理方法(good)
  11. History对象和location对象
  12. DB2 911错误的解释
  13. linux 一键安装lnmp环境
  14. AutoCAD开发5--批量修改dwg文件
  15. Spring MVC & Boot & Cloud 技术教程汇总(长期更新)
  16. ok,机房小感
  17. firefox(火狐)和Chrome(google)浏览器清空缓存操作的方法指引
  18. mysql datetime与timestamp精确到毫秒的问题
  19. mysql5.6常用查询sql
  20. Management Studio 插件生成安装包要点(以ProjkyAddin为例)

热门文章

  1. 深入理解JavaScript系列(16):闭包(Closures)
  2. 动作方法中 参数,Json
  3. JavaScript数组常用操作总结
  4. 【转】sqlserver字符串拆分(split)方法汇总
  5. <转>MapReduce工作原理图文详解
  6. java 判断图片的类型
  7. HTML 提高页面加载速度的方法
  8. 在Windows 7 上安装 Mapnik
  9. 刚在虚拟机上装的Linux系统,ifconfig后IP地址怎么成了127.0.0.1了
  10. 我的Android开发之路——ListView的使用