zoukankan      html  css  js  c++  java
  • [原创] Selenium By 查找页面元素

    通常selenium By 可以通过id,class,xpath,css等定位页面元素。

    id 和class比较简单,此处略....

    介绍下xpath的常用内置函数 contains()/not (contains()), starts-with()/ends-with(), position(), last()...

    代码如下

        @FindBy(how = How.XPATH, using = "//span[starts-with(@id,'bn_id')]/span[contains(@class,'bn_class')]")
        private List<WebElement> bnSpan;
        @FindBy(how = How.XPATH, using = "//div[contains(text(),'report')]") 
        private WebElement reportXpath;
    @FindBy(how = How.XPATH, using = "//span[not(contains(@class,'bn_classB'))]")
        private List<WebElement> bnXpath;

    position()--指定该div的排位

        @FindBy(how = How.XPATH, using = "//li[@id='li_id']/div[position()=1]")
        private List<WebElement> firstDiv;

    last() --指定最后一个节点,

         driver.findElement(By.xpath("(//div[@class='bt_class'])[last()]"))
    

      

    ps. 用xpath定位元素,若验证其正确与否,可在console使用方法$x(xpath).  e.g. :$x("//div[@id='ur']")

    再简单说下css,个人总结其语法规则

    空格后面是tag名

    #后为id

    . 后为class

    例如 用css定位如下Yes 的button, 其值可写成

    .class_6 button.btn-yes

  • 相关阅读:
    java 基础(Collections 工具类的常用方法)
    Java 基础( Map 实现类: Properties)
    Java 基础(Map)
    Java 基础(集合三)
    Java 基础(集合二)
    Java 基础(集合一)
    Java 基础(Annotation)
    CentOS7 系统 springboot应用启动脚本
    GenericObjectPool源码分析
    问题springboot项目启动报错
  • 原文地址:https://www.cnblogs.com/GGHHLL/p/2636260.html
Copyright © 2011-2022 走看看