Date picker interaction in Selenium Python - Build an Automation Script
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Initialize the Chrome driver with webdriver.Chrome() as driver: driver.get('https://example.com/date-picker') # Replace with actual URL wait = WebDriverWait(driver, 10) # Step 1: Locate the date picker input field date_input = wait.until(EC.element_to_be_clickable((By.ID, 'datePickerInput'))) # Step 2: Click to open the calendar widget date_input.click() # Step 3: Navigate to the desired month and year if needed # For simplicity, assume the calendar shows July 2024 by default # Step 4: Click on the day '15' day_15 = wait.until(EC.element_to_be_clickable((By.XPATH, "//td[@role='gridcell' and text()='15']"))) day_15.click() # Step 5: Verify the input field value updates selected_date = date_input.get_attribute('value') assert selected_date == '2024-07-15', f"Expected date '2024-07-15' but got '{selected_date}'" print('Test passed: Date picker selected date correctly.')
This script uses Selenium WebDriver with Python to automate the date picker interaction.
We start by opening the browser and navigating to the page with the date picker.
We wait explicitly for the date picker input field to be clickable, then click it to open the calendar.
Assuming the calendar shows the correct month and year, we locate the day '15' cell and click it.
Finally, we get the value of the input field and assert it matches the expected date '2024-07-15'.
Explicit waits ensure the elements are ready before interacting, avoiding flaky tests.
Now add data-driven testing to select three different dates: '2024-07-15', '2024-08-01', and '2024-12-25'.
Practice
Solution
Step 1: Understand date picker role
Date pickers let users pick a date easily and show it in an input field.Step 2: Verify date selection in tests
Tests should select a date and check if the input field shows the chosen date correctly.Final Answer:
To select a date and verify it appears correctly in the input field -> Option CQuick Check:
Date picker test = select and verify date [OK]
- Only checking visibility without selecting date
- Ignoring input field verification
- Testing unrelated UI changes like color
- Assuming page title changes after date pick
Solution
Step 1: Click date picker input by ID
Use By.ID with 'dateInput' to click the input field correctly.Step 2: Select date by visible text
Use XPath to find the table cell with text '15' and click it to select the date.Final Answer:
driver.find_element(By.ID, 'dateInput').click()\ndriver.find_element(By.XPATH, "//td[text()='15']").click() -> Option DQuick Check:
Correct locators and click actions = driver.find_element(By.ID, 'dateInput').click() driver.find_element(By.XPATH, "//td[text()='15']").click() [OK]
- Using CLASS_NAME instead of ID for input
- Trying to send_keys directly to date cell
- Using non-existent select() method
- Wrong locator for date cell
driver.find_element(By.ID, 'dateInput').click()
driver.find_element(By.XPATH, "//td[text()='10']").click()
selected_date = driver.find_element(By.ID, 'dateInput').get_attribute('value')
print(selected_date)Solution
Step 1: Click input and select date '10'
The code clicks the input and selects the date cell with text '10'.Step 2: Get input value attribute
After selection, the input's value attribute usually holds the full date string, e.g., '2024-10-10'.Final Answer:
2024-10-10 -> Option AQuick Check:
Input value after date pick = full date string [OK]
- Expecting only day number as input value
- Assuming input remains empty
- Confusing None with empty string
- Not retrieving 'value' attribute
date_input = driver.find_element(By.ID, 'dateInput') date_input.click() driver.find_element(By.XPATH, "//td[text()='25']").send_keys(Keys.ENTER)
Solution
Step 1: Identify element receiving send_keys
The code sends ENTER key to a table cell (td), which is not an input field.Step 2: Understand send_keys usage
send_keys works on input or editable elements, not on static table cells; clicking is needed instead.Final Answer:
Using send_keys(Keys.ENTER) on a non-input element -> Option AQuick Check:
send_keys on non-input element causes failure [OK]
- Trying to send_keys to non-input elements
- Forgetting to import Keys
- Assuming locator is wrong without checking
- Ignoring wait for picker visibility
Solution
Step 1: Click 'Next' button to go to next month
Click the button with ID 'nextMonthBtn' to change the calendar to next month.Step 2: Click date '1' in the updated calendar
After calendar updates, click the table cell with text '1' to select the date.Final Answer:
driver.find_element(By.ID, 'nextMonthBtn').click()\ndriver.find_element(By.XPATH, "//td[text()='1']").click() -> Option BQuick Check:
Navigate then select date = driver.find_element(By.ID, 'nextMonthBtn').click() driver.find_element(By.XPATH, "//td[text()='1']").click() [OK]
- Selecting date before navigating to next month
- Using send_keys instead of click on buttons
- Ignoring calendar update after clicking next
- Wrong order of actions
Ready to prove it?
Pass the test and this pattern moves unknown
