简体   繁体   English

如何使用Java和Selenium2 / Webdriver将明天的日期输入文本字段

[英]How can I enter tomorrow's date into a text field using Java and Selenium2/Webdriver

I am writing an automated test in Java using Selenium2/WebDriver. 我正在使用Selenium2 / WebDriver在Java中编写自动化测试。 I need to validate a birthday in the future is not allowed. 我需要在将来验证生日是不允许的。 To get tomorrow's date, I am using: 为了明天的约会,我正在使用:

Calendar calendar = Calendar.getInstance();
Date today = calendar.getTime();
calendar.add(Calendar.DAY_OF_YEAR, 1);

I am having trouble printing that to a text field, since .sendKeys requires characters. 我无法将其打印到文本字段,因为.sendKeys需要字符。 Any help is appreciated. 任何帮助表示赞赏。 I'm also unsure if that is the best way to get tomorrow's date. 我也不确定这是否是获得明天约会的最佳方式。

Your code is basically right. 你的代码基本上是对的。 Use a Calendar to produce Date objects: 使用Calendar生成Date对象:

Calendar calendar = Calendar.getInstance();
Date today = calendar.getTime();

calendar.add(Calendar.DAY_OF_YEAR, 1);
Date tomorrow = calendar.getTime();

Use SimpleDateFormat to format the Date as a String : 使用SimpleDateFormatDate格式化为String

DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");

String todayAsString = dateFormat.format(today);
String tomorrowAsString = dateFormat.format(tomorrow);

System.out.println(todayAsString);
System.out.println(tomorrowAsString);

Prints: 打印:

09-Aug-2012
10-Aug-2012

You can use Selenium to send those String objects to the date control (if it accepts keyed input, of course). 您可以使用Selenium将这些String对象发送到日期控件(当然,如果它接受键控输入)。 You'll need to adjust the date pattern "dd-MMM-yyyy" to match the format expected by the input control on the page, eg perhaps it's ( "MM/dd/YY" )? 您需要调整日期模式"dd-MMM-yyyy"以匹配页面上输入控件所期望的格式,例如,它可能是( "MM/dd/YY" )?

First, don't you mean to add before defining the date? 首先,您不是要在定义日期之前添加吗? Then, you could do a 然后,你可以做一个

today.toString()

to assign via sendkeys, but IMO you should look first at the format the widget accepts (eg european vs US) and format your date accordingly (eg using something like this ). 通过的SendKeys分配,但IMO你应该先了解一下控件接受格式(例如欧洲VS美国)和(例如,使用类似相应的格式化你的日期 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Java在Selenium2(Webdriver)中键入Gmail正文文本 - How to type Gmail Body text in Selenium2 (Webdriver) using Java 在 Eclipse 中使用 Selenium WebDriver 和 java 代码在搜索字段中输入文本后如何按“Enter” - How to press 'Enter' once text is entered in the search field using Selenium WebDriver and java code in eclipse 使用Java的Selenium WebDriver(aka Selenium2)中的getPageSource() - getPageSource() in Selenium WebDriver(a.k.a Selenium2) using Java 无法使用带有 Java 的 Selenium2 (Webdriver) 启动 IE 浏览器 - Not able to launch IE browser using Selenium2 (Webdriver) with Java 当浏览器窗口未聚焦时,使用Java的Selenium2(WebDriver)失败 - Selenium2 (WebDriver) using Java fails when browser window is not focused 如何使用Selenium2(java)输入iframe? - How to type in iframe using selenium2 (java)? 如何使用 Java 在 Selenium WebDriver 的隐藏字段中输入一些文本 - How to type some text in hidden field in Selenium WebDriver using Java 如何在 selenium webdriver 的文本字段中一个一个地输入字符? - How to enter characters one by one in to a text field in selenium webdriver? 如何使用Selenium Webdriver和Java从元素中获取文本? - How can I get text from element with Selenium webdriver and Java? 如何在Selenium2(Webdriver)中处理POPUP浏览器? - How to Handle POPUP browser in Selenium2(Webdriver)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM