A date picker is a user interface element that allows users to select a date from a calendar popup or dropdown. In testing, it requires special handling to select dates correctly.
Click to reveal answer
beginner
Name two common strategies to automate date picker selection in Selenium.
1. Directly inputting the date string into the date field if allowed. 2. Navigating the calendar UI by clicking next/previous buttons and selecting the date element.
Click to reveal answer
intermediate
Why might directly sending keys to a date picker input fail?
Because some date pickers are read-only or use custom UI controls that ignore keyboard input, so sending keys won't update the date properly.
Click to reveal answer
intermediate
How can you handle dynamic calendar UI elements in Selenium for date pickers?
By locating calendar controls using stable locators like aria-labels or data attributes, then using loops to navigate months and select the correct day element.
Click to reveal answer
beginner
What is a best practice when writing locators for date picker elements?
Use unique, stable attributes such as aria-labels, data-* attributes, or visible text instead of brittle XPath or CSS selectors that rely on position.
Click to reveal answer
Which strategy is best if the date picker input field is editable?
ASend keys directly to the input field
BClick through calendar UI controls only
CUse JavaScript to change the date value
DIgnore the date picker and skip the test
✗ Incorrect
If the input field accepts text, sending keys is the simplest and fastest way to set the date.
What is a common problem when clicking calendar UI elements in Selenium?
ACalendar UI never changes dynamically
BSelenium cannot find any elements on the page
CDate pickers always accept keyboard input
DElements may not be visible or clickable immediately
✗ Incorrect
Calendar elements may load dynamically or be hidden, so waits or checks are needed before clicking.
Why avoid using absolute XPath for date picker elements?
AXPath is not supported in Selenium
BThey are slow to execute
CThey break easily if UI changes
DThey do not work with calendars
✗ Incorrect
Absolute XPath depends on exact UI structure, which often changes, making tests fragile.
Which locator attribute is best for accessibility and stable selection?
Astyle
Baria-label
Conclick
Dclass (if generic)
✗ Incorrect
aria-label attributes describe elements for screen readers and are usually stable and unique.
What should you do if the date picker uses a custom widget that blocks keyboard input?
AUse Selenium to click through the calendar UI
BSend keys to the input field anyway
CSkip testing the date picker
DUse alert popups to enter date
✗ Incorrect
Clicking through the calendar UI simulates user behavior and works with custom widgets.
Explain how you would automate selecting a date in a date picker that does not allow typing.
Think about how a user clicks through the calendar to pick a date.
You got /4 concepts.
List best practices for writing locators for date picker elements in Selenium.
Focus on locator stability and accessibility.
You got /4 concepts.
Practice
(1/5)
1. What is the main reason to use a date picker in a web form during testing?
easy
A. To speed up page loading time
B. To allow users to select a date easily and avoid manual input errors
C. To change the website's theme colors
D. To validate user passwords
Solution
Step 1: Understand the purpose of a date picker
A date picker helps users select dates without typing, reducing errors.
Step 2: Identify the testing goal
Testing ensures the date picker works correctly and inputs valid dates.
Final Answer:
To allow users to select a date easily and avoid manual input errors -> Option B
Quick Check:
Date picker purpose = Easy date selection [OK]
Hint: Date pickers help avoid typing mistakes [OK]
Common Mistakes:
Confusing date picker with unrelated UI elements
Thinking date picker changes page speed
Assuming date picker validates passwords
2. Which Selenium Java code snippet correctly clicks a date picker input field using its id 'dateInput'?
easy
A. driver.findElement(By.tagName("dateInput")).click();
B. driver.findElement(By.name("dateInput")).click();
C. driver.findElement(By.className("dateInput")).click();
D. driver.findElement(By.id("dateInput")).click();
Solution
Step 1: Identify the locator strategy for id
Use By.id("dateInput") to find element by its id attribute.
Step 2: Use click() method on the found element
Calling click() simulates user clicking the date picker input.
Final Answer:
driver.findElement(By.id("dateInput")).click(); -> Option D
Quick Check:
Use By.id for id attribute [OK]
Hint: Use By.id for id locators in Selenium [OK]
Common Mistakes:
Using By.name when id is given
Using By.className incorrectly
Using By.tagName with id value
3. Given this Selenium Java code snippet, what will be the output if the date picker input's value is set correctly?
A. The date picker input is inside an iframe; switch to iframe before accessing it
B. The xpath syntax is incorrect; use By.id instead
C. sendKeys cannot be used on input fields
D. The date format "2024-12-01" is invalid
Solution
Step 1: Understand NoSuchElementException means element not found
This often happens if element is inside an iframe or not visible yet.
Step 2: Check if date picker is inside iframe and switch context
Switching to iframe with driver.switchTo().frame(...) allows access to elements inside it.
Final Answer:
The date picker input is inside an iframe; switch to iframe before accessing it -> Option A
Quick Check:
Switch to iframe to find elements inside [OK]
Hint: Check if element is inside iframe before locating [OK]
Common Mistakes:
Assuming xpath is always wrong
Thinking sendKeys can't be used on inputs
Blaming date format for element not found
5. You want to select a date 10 days from today using Selenium Java on a date picker that only allows clicking days (no typing). Which strategy is best?
hard
A. Calculate the target date, open the date picker, then click the day button matching that date
B. Use sendKeys to type the date string directly into the input field
C. Reload the page and hope the default date is correct
D. Use JavaScript to set the input value without clicking
Solution
Step 1: Calculate the date 10 days from today in Java
Use LocalDate.now().plusDays(10) to get the target date.
Step 2: Open the date picker and click the day matching the target date
Locate the day button by text or attribute and click it to select.
Final Answer:
Calculate the target date, open the date picker, then click the day button matching that date -> Option A
Quick Check:
Click matching day for pickers without typing [OK]
Hint: Click the correct day button after calculating date [OK]
Common Mistakes:
Trying to type date when input disallows it
Ignoring date calculation and clicking wrong day
Using JavaScript to set value without UI interaction
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.