3

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

HRVHackers
  • 2,793
  • 4
  • 36
  • 38

4 Answers4

2

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.

Amrit
  • 433
  • 3
  • 19
  • Hmm this seems to be helping. I'll close this out for now and will re-open if it rears its ugly head again. – HRVHackers May 14 '14 at 15:20
2

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()");
Chris Stubbs
  • 358
  • 2
  • 10
1

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");
ddavison
  • 28,221
  • 15
  • 85
  • 110
  • Thanks, but the locator is correct. Before webElement is passed into the function, I check that it's both visible and present in an earlier method. data-model-attribute is from a parent element and works fine with other text fields; it only randomly fails with date fields – HRVHackers Apr 17 '14 at 20:29
  • btw, I was just checking out your profile on air pair, and here you are attempting at answering my question. Surely the programming gods have brought us together :) – HRVHackers Apr 18 '14 at 00:13
0

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.

DMart
  • 2,401
  • 1
  • 14
  • 19
  • That sounded good but didn't work unfortunately. Also the app expects the date to be entered as as MM/DD/YYYY when I entered it as YYYY-MM-DD, the result wasn't a proper date: *Thread:1 INFO: EnteringText: 1981-11-01 *Thread:1 INFO: Text Entered was: 11101-12-08 – HRVHackers Apr 18 '14 at 06:53
  • Where is the "Text Entered" line coming from its not in the code snippet above? Is it possible this field is readonly? http://stackoverflow.com/questions/12419339/not-able-to-input-date-using-sendkeys-in-selenium-webdriver – DMart Nov 26 '14 at 16:31