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

  • 相关阅读:
    中国软件技术篇
    create pdf
    我来了
    补丁号
    rdlc里面用VB语法对数值型数据进行Format
    WinForm中有些控件不能添加ToolTip关联的解决方法
    javascript弹出对话框
    省市县关联菜单
    委托与事件
    ASP.NET多文件上传方案
  • 原文地址:https://www.cnblogs.com/GGHHLL/p/2636260.html
Copyright © 2011-2022 走看看