Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to input date using sendKeys in Selenium WebDriver

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.

like image 227
Suchitra R.D Avatar asked Sep 14 '12 06:09

Suchitra R.D


2 Answers

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
like image 108
Prashant Vadher Avatar answered Oct 07 '22 05:10

Prashant Vadher


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");
like image 38
kushan Avatar answered Oct 07 '22 04:10

kushan