Selenium Date Picker Automation: Why clear() Fails and how to fix It (React DatePicker Case)
Automation Engineer | Java + Selenium | Appium + Java | RestAssured | Sharing real-world automation tips
while automating a date picker recently, I ran into an issue that looks simple but wastes a lot of debugging time:
dateInputField.clear();
Instead of clearing the field, the calendar popup kept appearing, and sometimes the previous value came back automatically.
If you’ve worked with modern UI frameworks like React, this behaviour will feel familiar.
Let’s break down what’s happening and the reliable fix.
The Problem
In many modern web apps, date pickers are not plain HTML inputs. They are React-Controlled components.
That means:
The UI controls the input value.
Clearing the field triggers internal events.
The component re-populates the value or opens the calender popup.
So this code:
dateInputField.clear();
dateInputField.sendKeys("10/15/2023");
often fails because:
Input regains focus.
Calendar popup opens.
Old value reaapears.
Typing becomes unreliable.
Why .clear() Doesn’t Work Realiably
clear() simulates user deletion, but React components:
Listen to change events.
Maintain internal state.
Re-render inout values automatically.
So even after clearing, React sometimes restores the value.
Reliable Solution: Clear Using JavaScript
The best solution is to bypass UI events and directly update the value.
Stable implementation
public void enterDate(String date) {
wait.until(ExpectedConditions.elementToBeClickable(dateInputField));
// Focus input
dateInputField.click();
// Clearing using JavaScript
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].value='';", dateInputField);
// Enter new date
dateInputField.sendKeys(date);
// Move focus away to close popup
dateInputField.sendkeys(Keys.TAB);
}
Why This Works
JavaScript directly modifies the value:
No keyboard events fired
No React re-render conflict
No popup triggered
Value updates cleanly
It behaves consistently across React-based pickers.
Bonus: Debug Tip
To confirm the value actually changes:
System.out.println(dateInputField.getAttribute("value"));
Sometimes UI shows a value, but Selenium still sees old one.
