• selenium java操作时间控件


    1、经过搜索可以有两种方法去实现。

    如果时间控件含有只读属性,,那么是不能通过sendkeys去赋值的。。需要通过js去掉readonly的只读属性,再进行赋值;

    //     JavascriptExecutor removeAttribute = (JavascriptExecutor)driver; 
    //    //remove readonly attribute
    //     removeAttribute.executeScript("var setDate=document.getElementById("startTime");setDate.removeAttribute('readonly');") ;
    //     this.start.sendKeys("2016-05-28 22:50:45");

    2、第二种方法就是直接点击。

       往上有人说时间控件需要跳出iframe。但是貌似我这里没有用到。

    我是通过css去定位了day。hour和minute

    另:先看下我这里的ui代码.去定位的时候,定位到了该蓝色input标签处,而实际的值是存储在下面的input处,经了解,上面的是用来显示的。下面的是真正用来赋值的。(其实这里是可以通过js的document命令去赋值的:

    document.getElementById('startTime').setAttribute('value','2016-05-12 09:50:40')

    也可以赋值成功;

    这就说明:可以巧妙的结合javascript脚本语言的document命令,获取明个元素并修改该元素的值,或者复制。这也可以使用在ui自动化测试中。

    常用的document的命令: 

    Document类型   

    特征

    属性

    documentElement

    body

    doctype

    title

    URL

    domain

    dir

    方法

     getElementById()

    getElementsByTagName()

    getElementsByName()

    getElementsClassName()

    createElement()

    createTextNode()

    createAttribute()
    ---------------------
    作者:平常心_c
    来源:CSDN
    原文:https://blog.csdn.net/qq_37674616/article/details/82392982
    版权声明:本文为博主原创文章,转载请附上博文链接!

    我是通过直接点击实现的。

    定位代码

    private WebElement endTime;
        @FindBy(css="div.datetimepicker:nth-child(6) > div:nth-child(3) > table:nth-child(1) > tfoot:nth-child(3) > tr:nth-child(1) > th:nth-child(1)")
        private WebElement today;
        @FindBy(css="div.datetimepicker:nth-child(7) > div:nth-child(3) > table:nth-child(1) > thead:nth-child(1) > tr:nth-child(1) > th:nth-child(3) > span:nth-child(1)")
        private WebElement rightBtn;
        @FindBy(css="div.datetimepicker:nth-child(7) > div:nth-child(3) > table:nth-child(1) > tbody:nth-child(2) > tr:nth-child(4) > td:nth-child(4)")
        private WebElement day;
        @FindBy(css="div.datetimepicker:nth-child(7) > div:nth-child(2) > table:nth-child(1) > tbody:nth-child(2) > tr:nth-child(1) > td:nth-child(1) > fieldset:nth-child(2) > span:nth-child(4)")
        private WebElement hour;
        @FindBy(css="div.datetimepicker:nth-child(7) > div:nth-child(1) > table:nth-child(1) > tbody:nth-child(2) > tr:nth-child(1) > td:nth-child(1) > fieldset:nth-child(1) > span:nth-child(8)")
        private WebElement time;

    方法代码

    public void startTime( )throws InterruptedException{
    //     JavascriptExecutor removeAttribute = (JavascriptExecutor)driver; 
    //    //remove readonly attribute
    //     removeAttribute.executeScript("var setDate=document.getElementById("startTime");setDate.removeAttribute('readonly');") ;
    //     this.start.sendKeys("2016-05-28 22:50:45");
        this.startTime.click();
        Thread.sleep(2000);  
    //输入今天
    this.today.click(); Thread.sleep(1000); } public void endTime( )throws InterruptedException{ this.endTime.click(); Thread.sleep(2000);
    //点击下一个月按钮
    this.rightBtn.click(); Thread.sleep(2000);
    //选择日
    this.day.click(); Thread.sleep(2000);
    //选择时
    this.hour.click(); Thread.sleep(2000);
    //选择时分秒
    this.time.click(); Thread.sleep(1000); }
  • 相关阅读:
    Java Web连接各种数据库方式汇总
    java.lang.UnsupportedClassVersionError: Bad version number in .class file 解决办法
    <meta 标签的详细使用
    Redis实战之征服 Redis + Jedis + Spring (一)
    Redis实战之征服 Redis + Jedis + Spring (二)
    Redis实战之征服 Redis + Jedis + Spring (三)
    快速组合数
    HDU4632:Palindrome subsequence(区间DP)
    阿里暂停微信服务:让商家回归微信的本职,做好客户服务
    谈谈无穷小微积分对我国微积分教学改革的影响
  • 原文地址:https://www.cnblogs.com/amy7758/p/5481210.html
Copyright © 2020-2023  润新知