11

The date field is like a calendar and I'm not able to input the date using sendKeys of Selenium WebDriver.

But "type" in the date field was working fine before with Selenium RC.

I tried using "clear()" before "sendKeys()" but this gave the error:

Caught Exception: Element is read-only and so may not be used for actions
Command duration or timeout: 10.11 seconds

sendKeys() is working fine for other text input fields.

I tried isDisplayed() to check for the element and it comes as true. Even in the browser, when running the test, the cursor goes to the date fields but doesnt type any text into them.

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Suchitra R.D
  • 180
  • 1
  • 3
  • 12
  • 1
    I had the same issue, but with Telerik's RadCalendar (.NET), for me the issue was worked around by physically using the calendar to simulate clicking a date. – Arran Sep 14 '12 at 13:36
  • 1
    I came across this blog [http://blog.reallysimplethoughts.com/2012/07/19/new-selenese-command-sendkeys/] This says that "sendKeys simulates a real user typing every character in the specified string; it is also bound by the limitations of a real user, like not being able to type into a invisible or read only elements." So, it seems typing into the date field is not possible for my app. Can anyone please suggest any other workarounds for selecting the date from calendar? – Suchitra R.D Sep 17 '12 at 06:48
  • A direct solution would be something like @Arran said. You should simulate the user selecting a date. I don't know of other workarounds. – Ignacio Contreras Pinilla Sep 17 '12 at 10:42
  • 2
    Since mine is data driven framework, simulating click on a date(dynamic) will be a bit difficult. I got this working by using JavascriptExecutor like: `((JavascriptExecutor)driver).executeScript("arguments[0].value=arguments[1]", driver.findElement(By.id(stringc3)), stringc4);` where stringc3 is the locator id of the date field and stringc4 is the date like "01-01-2012" – Suchitra R.D Sep 21 '12 at 05:01

5 Answers5

8

Use Following Code for this...

((JavascriptExecutor)driver).executeScript ("document.getElementById('dateofbirth').removeAttribute('readonly',0);");

WebElement BirthDate= driver.findElement(By.id("dateofbirth"));
BirthDate.clear();
BirthDate.sendKeys("20-Aug-1985"); //Enter this date details with valid date format
Shoaib Shaikh
  • 343
  • 7
  • 22
Prashant Vadher
  • 1,057
  • 11
  • 9
3

I also faced the same issue.This is what the solution I found.This worked fine for me. Just remove the read only attribute of the input field and then do just as other input fields.

   ((JavascriptExecutor) driver).executeScript("document.getElementsByName('date'[0].removeAttribute('readonly');");
    WebElement dateFld = driver.findElement(By.id("date_completed"));
    dateFld.clear();
    dateFld.sendKeys("date Completed");
kushan
  • 775
  • 2
  • 10
  • 19
3

For future readers of this thread, the solution posted by @Flaburgan for issue https://github.com/mozilla/geckodriver/issues/1070 was found to work with Firefox 63 on Win7-64

"For the record, it looks like send_keys with a correctly formatted ISO date (yyyy-mm-dd) works. So for your example, can you please try to call send_keys with (like) 2012-11-02?"

Snidhi Sofpro
  • 479
  • 7
  • 10
1

If You are using a jQuery date picker object, the field should be read-only and date have to be selected from calendar object. In that case You can use 'Select' class methods of Selenium Web Driver to choose a date.

Select date = new Select(driver.findElement(By.linkText("the date want to select")));
date.click();
Deepu
  • 113
  • 2
  • 16
0

I have done this and works great. Take care of the format. By this you can even get the value from the control.

var dob = element(by.id('dateOfBirth'))
dob.sendKeys('20-08-1985');

expect(element(by.id('dateOfBirth')).getAttribute('value')).toBe('2015-20-08');

Hope it helps.

Sandy
  • 11,332
  • 27
  • 76
  • 122