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); }

 

转载于:https://www.cnblogs.com/amy7758/p/5481210.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值