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