Not able to input date using sendKeys in Selenium WebDriver - java

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.

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

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");

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?"

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();

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.

Related

Issue with selecting date from calendar in Selenium

I'm trying to select current date from the calendar on this website "www.makemytrip.com".
Using these 2 lines of code:
driver.findElement(By.xpath("//label[#for='departure']")).click();
To open the calendar and to select date:
driver.findElement(By.cssSelector(".DayPicker-Day.DayPicker-Day--selected.DayPicker-Day--today")).click();
The first one is working fine as it opens up the calendar but cssSelector is not responding. I've tried various variations but still it remains unresponsive.
Try this xpath strategy:
//div[#class='DayPicker-Day DayPicker-Day--today']
Now, i write in python, so you may translate my code to Java, since most of it remains same.
time.sleep (Thread.sleep in Java) here is optional Ideally you should use WebdriverWait instead of Thread.sleep. But just to show you, I used it.
driver.get('https://www.makemytrip.com/')
time.sleep(3)
driver.find_element(By.XPATH, "//label[#for='departure']").click()
time.sleep(1)
dx = driver.find_element(By.XPATH, "//div[#class='DayPicker-Day DayPicker-Day--today']")
print(dx.text)
dx.click()
Here is the output:
17 is the date and the other value is currency.
17
₹ 9,418
Process finished with exit code 0

How to retrieve date only from date cells [duplicate]

Currently i have my code as
bean.setREPO_DATE(row.getCell(16).getDateCellValue());
it works fine if cell is formatted as date in excel.
However it also converts some integer or long like 1234 or 5699 to date. I know the reason behind this too.
However i want to apply a check before executing above line. Something like this
if(row.getCell(16).isOfDateFormat){
bean.setREPO_DATE(row.getCell(16).getDateCellValue());
}
Please Guide me..
Thanks in Advance !
Try this,
use import org.apache.poi.ss.usermodel.DateUtil;
if(DateUtil.isCellDateFormatted(cell))
{
cell.getDateCellValue();
}

Java: String equals not working for innerHTML text

I am using Selenium in java for testing a search wizard where I need to click on a current date. I am trying to traverse through all the dates in the calendar and if matches the current date, I click it.
The problem here is that the dates are hidden so I have to get the innerHTML attribute, then extract the date number using substring and then comparing it with Calendar.getInstance().get(Calendar.DAY_OF_MONTH) but my condition is failing every time. Have tried str.trim() and also tried to remove all the unicode characters but nothing worked. Below is the code:
WebElement day_of_month = AutoUtils.findElementByTagName(day, "span");
if(day_of_month!=null){
String innerHtml = day_of_month.getAttribute("innerHTML");
// innerHtml is "23<span class="buzz-message">Great price</span><i></i>"
// where 23 is the date i need
String exact_date = innerHtml.substring(0,innerHtml.indexOf("<span"));
exact_date = exact_date.replaceAll("\\P{Print}", ""); // have also tried exact_date.trim();
if(exact_date.equals(Calendar.getInstance().get(Calendar.DAY_OF_MONTH))){
day_of_month.click();
} else{
System.out.Println("not found"); //gets printed every time
}
}
Can somebody help?
You have to convert the int value (day of month) to String:
if (exact_date.equals(String.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_MONTH)))) {
...
}
try using innerText instead of innerHTML . innerText only gives you what is visible to the user . innerHTML gives even html and javascripts inside it.
You should be able to get "23" only and do a easy comparison.
Also, if you can show the html , it might be easy for us to help you .

How to change date in datepicker using Appium

I'm trying to change the date in a datepicker element using appium. I can't use the findElement(By.id("id"); since I'm running in version 4.2.2(API 17) and as far as i know By.id is not supported in this version. Using selendroid i can change the date like this:
driver.findElement(By.id("MONTH")).sendKeys("FEB");
In my code in appium I try to access it by the default date. For example im trying to change the month like this:
driver.findElement(By.name("Dec")).sendKeys("Jan");
It seems that although it finds the name Dec it can't send the keys Jan.
Here is the error from the Failure Trace:
org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)
Any ides on how i can change it and generally if i can use somehow the findElement(By.id("id"); in this API version?
Thanks!!
Instead of using the By.name i used By.className in order to change the month the day and the year. Here is my code:
List<WebElement> date = driver.findElements(By.className("android.widget.NumberPicker"));
date.get(0).sendKeys("Apr");
date.get(1).sendKeys("17");
date.get(2).sendKeys("1972");

Display tag show user's local time in browser Jsp

I am using display tag for table details i want to change date & time to user's local browser setting, Data in DB is UMT. IF user is from india then it will add +5:30 etc. How can i do this. I am stuck in this. Please help me.
Have you tried like this ??
Locale cLoc=request.getLocale();
Calendar cCal=Calendar.getInstance(cLoc);
TimeZone tz=cCal.getTimeZone();
//......
A bit late to the party, but still answer here in case anyone else needs it.
Display.tag uses MessageFormatColumnDecorator internally to apply MessageFormat formatter to all columns that have "format" parameter set.
So, your options are:
Create your own custom MessageFormatColumnDecorator. Unfortunatelly it is inined in the ColumnTag code and cannot be very easily replaced (decorators are chained), so you will have to compile your own copy of Display.Tag jar, with your custom decorator in it.
In your custom decorator use the following sample code to change MessageFormat's timezone:
TimeZone tz = //get your timezone here
Object [] formats = format.getFormats(); //'format' is MessageFormat instance
for (int i = 0; i < formats.length; i++) {
if (formats[i] instanceof SimpleDateFormat) {
((SimpleDateFormat)formats[i]).setTimeZone(tz);
}
}
Second option is to use the <fmt:formatDate> within Display.tag column tags to format the output.
Sample Code:
<display:table name="dateList" id="object">
<display:column title="Date">
<fmt:formatDate value="${object.date}" type="both" dateStyle="long" timeStyle="long"/>
</display:column>
</display:table>
You can set default request locale for <fmt:formatDate> or wrap them in <fmt:timeZone> tag to set a timezone for them to use.

Categories