zoukankan      html  css  js  c++  java
  • 元素定位

    1、id:find_element_by_id()  

    当id属性 是唯一的不变的时候,可使用这个定位元素。

    如:<input id="username" class="ant-input ant-input-lg" value="" data-__meta="[object Object]" autocomplete="off" type="text"/>

     find_element_by_id("username")

    2、name:find_element_by_name()

    当name属性 是唯一的不变的时候,可使用这个定位元素。

    如:<input id="username" name="kw" class="ant-input ant-input-lg" value="" data-__meta="[object Object]" autocomplete="off" type="text"/>

    find_element_by_name("kw")

    3、class:find_element_by_class_name()  

    当class 属性 是唯一的不变的时候,可使用这个定位元素。

    如:<input id="username" name="kw" class="ant-input ant-input-lg" value="" data-__meta="[object Object]" autocomplete="off" type="text"/>

    find_element_by_class_name("ant-input ant-input-lg")

    4、tag(标签名):find_element_by_tag_name()  

    很少用这个定位,一般标签名都会有多个。

    如:<input id="username" name="kw" class="ant-input ant-input-lg" value="" data-__meta="[object Object]" autocomplete="off" type="text"/>

    find_element_by_tag_name("input")

     5、link(链接):find_element_by_link()/find_element_by_partial_link()

    对于一个元素属性有:href=’',表示他是一个超链接,就可以通过这个区定位

    如: <a href="#/data/query/allitem">

    find_element_by_link("#/data/query/allitem")  ---精确匹配

    find_element_by_partial_link("#/data/query")  ---模糊匹配

    6、css定位:find_element_by_css_selector()

    比较灵活可以随便选择属性定位。

    如:<input id="username" name="kw" class="ant-input ant-input-lg" value="" data-__meta="[object Object]" autocomplete="off" type="text"/>

    通过id:find_element_by_css_selector("input[id="username"]")

    通过name:find_element_by_css_selector("input[name="kw"]")

    ............

    其他属性大致一样

    7、xpath定位:find_element_by_xpath()

    通过元素的路径来查找这个元素。

    ① 属性和标签

    如:<input id="username" name="kw" class="ant-input ant-input-lg" value="" data-__meta="[object Object]" autocomplete="off" type="text"/>

    通过id:find_element_by_xpath("//*[@id='username']")    ----  *  表示查询所有标签中id为username的元素

    通过name:find_element_by_css_selector("//input[@name=‘kw’]")   ----  input 表示指定查找input标签中name为kw的元素

    .......

    通过其他属性查询大致一样。

    ②层级

    若一个元素的属性不是很明显,无法直接定位到,这是可以先找到他的父节点,然后再找下个层级就可以定位到了。若父节点还是不明显,就再往上一层级找........

    如:

    通过节点查找:find_element_by_xpth("//span[@class='ant-input-wrapper']/input)

    通过再上一层级查找:find_element_by_xpth("//div[@class='ant-form-item-control']/span/input")

    ③索引

    一个元素他的兄弟元素跟他的标签一样,这时无法通过层级查找到,这时就可以通过索引的方式(因为有先后差别),索引是从1开始的

     

    通过索引查找:find_element_by_xpth("//u1[@role='menu']/li[1]")

    ④逻辑运算

    支持多个属性逻辑运算的,与(and)、或(or)、非(not)

    如:<input id="username" name="kw" class="ant-input ant-input-lg" value="" data-__meta="[object Object]" autocomplete="off" type="text"/>

    find_element_by_xpath("//*[@id='username' and name=‘kw’]")    ----  and运算

    ⑤模糊匹配

    find_element_by_xpath("*[contains(text(),'hao123')]")    -----模糊匹配功能

    find_element_by_xpath("*[contains(@id,'username')]")  -----模糊匹配某个属性

    find_element_by_xpath("*[start-with(@id,'username')]")  ------模糊匹配已什么开头

    .........

    ⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳

  • 相关阅读:
    一个简单例子:贫血模型or领域模型
    eclipse从数据库逆向生成Hibernate实体类
    Hibernate unsaved-value 属性
    webservice和restful的区别
    Web Service 的工作原理
    Hibernate3的DetachedCriteria支持
    hibernate criteria中Restrictions的用法
    Google Gson 使用简介
    struts2 访问国际化资源 <s:text>作为属性
    EL表达式从request和session中取值
  • 原文地址:https://www.cnblogs.com/cxx1/p/7066860.html
Copyright © 2011-2022 走看看