Reputation: 2873
I have a date field that sometimes will get filled out by Webdriver element.sendKeys() and other times the field will just get skipped. (using Chromedriver 2.9).
Element Locator Info:
<input id="dateOfBirth" type="date" class=" form-control " placeholder="">
The code for Entering text and re-trying if fails:
//Web element already verified present and visible before being passed into the method
public static void enterText(WebElement weElement, String textToEnter) {
System.out.println(" *Thread:" +Thread.currentThread().getId() +" INFO: EnteringText: " +textToEnter);
//weElement.click();
weElement.sendKeys(textToEnter);
String textEntered = weElement.getAttribute("value");
System.out.println(" *Thread:" +Thread.currentThread().getId() +" INFO: TextDisplayed is: "+ textEntered);
//continued
int iAttempts = 0;
while (iAttempts < 1) {
if(!textEntered.isEmpty())
break;
else{
System.out.println(" *Thread:" +Thread.currentThread().getId() +" ERROR: re-Attempting to enter text: "+ textToEnter);
//weElement.click();
weElement.sendKeys(textToEnter);
textEntered= weElement.getAttribute("value");
System.out.println(" *Thread:" +Thread.currentThread().getId() +" INFO: Element text after re-attempt: "+ textEntered);
iAttempts++;
}
}
}
The console output:
*Thread:10 Trying: com.xxx.pageobjects.IdentityPage.typeDOB
*Thread:10 INFO: Locator is: [data-model-attribute='dateOfBirth'] input
*Thread:10 INFO: EnteringText: 01/01/1981
*Thread:10 INFO: TextDisplayed is:
*Thread:10 ERROR: re-Attempting to enter text: 01/01/1981
*Thread:10 INFO: Element text after re-attempt:
Does anyone have any ideas as to why this happens only with date fields? And any ideas for a better workaround in case it fails? Thanks!
Note: My app only works on Chrome so I am unable to confirm if the issue happens in other browsers
Upvotes: 3
Views: 1588
Reputation: 358
I got around this issue by executing some javascript to fill out date fields.
protected void FillOutDate(string cssSelector, DateTime date)
{
var js = Driver as IJavaScriptExecutor;
if (js != null) js.ExecuteScript(string.Format("$('{0}').val('{1}').change()", cssSelector,date.ToString("yyyy-MM-dd")));
}
OR simply
((IJavaScriptExecutor)Driver).ExecuteScript("$('#IdSelector').val('2014-06-11').change()");
Upvotes: 2
Reputation: 443
We are getting the same error with the date field. Particularly with recent chrome browser upgrade to version 34. Try rollback to version 33. It should work.
Upvotes: 2
Reputation: 2461
I believe the input type of "date" is new to html 5 and requires a specific to the RFC 3339: http://www.ietf.org/rfc/rfc3339.txt
Try using 1981-01-01 and it should work. YYYY-MM-DD instead of MM/DD/YYYY as you have provided.
Upvotes: 0
Reputation: 29032
According to your console output, you are using an incorrect locator. Seeming is that I don't see the data-model-attribute
attr anywhere, let's use the ID.
enterText(driver.findElement(By.id("dateOfBirth")), "test");
Upvotes: 1