selenium xpath定位

id,  name,  class name,  tag name,css

link text,  partial link text,  xpath,  css selectorhtml

 

下面主要介绍一下xpath:函数

 

1、xpath基本定位用法3d

  1.1 使用id定位 -- driver.find_element_by_xpath('//input[@id="kw"]')code

  

  1.2 使用class定位 -- driver.find_element_by_xpath('//input[@class="s_ipt"]')orm

  

  1.3 固然 经过经常使用的8种方式结合xpath都可以定位(name、tag_name、link_text、partial_link_text)以上只列举了2种经常使用方式哦。htm

2、xpath相对路径/绝对路径定位blog

  2.1 相对定位 -- 以// 开头 如://form//input[@name="phone"]ip

  

  2.2 绝对定位 -- 以/ 开头,可是要从根目录开始,比较繁琐,通常不建议使用 如:/html/body/div/aci

  

3、xpath文本、模糊、逻辑定位

  3.1【文本定位】使用text()元素的text内容 如://button[text()="登陆"]

  

  3.2 【模糊定位】使用contains() 包含函数 如://button[contains(text(),"登陆")]、//button[contains(@class,"btn")] 除了contains不是=等于

  

  3.3 【模糊定位】使用starts-with -- 匹配以xx开头的属性值;ends-with -- 匹配以xx结尾的属性值  如://button[starts-with(@class,"btn")]、//input[ends-with(@class,"-special")]

  3.4  使用逻辑运算符 -- and、or;如://input[@name="phone" and @datatype="m"]

4、xpath轴定位

  4.1  轴运算

  ancestor:祖先节点 包括父
  parent:父节点
  preceding-sibling:当前元素节点标签以前的全部兄弟节点
  preceding:当前元素节点标签以前的全部节点 
  following-sibling:当前元素节点标签以后的全部兄弟节点
  following:当前元素节点标签以后的全部节点 
 
  使用语法: 轴名称 :: 节点名称
  使用较多场景:页面显示为一个表格样式的数据列
  如:
  

  

注意:

#定位 找到元素 -- 作到惟一识别#优先使用id#舍弃:有下标的出现、有绝对定位的出现、id动态变化时舍弃其它文章:https://www.cnblogs.com/hanmk/p/8997786.html        https://www.cnblogs.com/hanmk/p/9015502.html
相关文章
相关标签/搜索