Last modified: Oct 23, 2024 By Alexander Williams

Python Selenium: Date Picker Handling

Date pickers are a common element in web forms and booking systems. Automating the process of selecting dates in Python Selenium can streamline your test or web scraping scripts.

Types of Date Pickers

Date pickers vary across websites. Some use simple input fields where dates can be typed directly, while others have interactive calendars where dates must be selected.

Handling Text-Based Date Pickers

Some date pickers allow users to directly input a date into a text field. In such cases, you can use Selenium’s sending keys method to input the date.


from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

# Initialize the driver
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

# Open the website with a date picker
driver.get('https://example.com/datepicker')

# Locate the date picker input field
date_input = driver.find_element(By.ID, 'datePicker')

# Clear the field and input a date
date_input.clear()
date_input.send_keys('10/22/2024')

# Submit the form or proceed with the next action
driver.find_element(By.ID, 'submit').click()

# Close the driver
driver.quit()

In this example, Selenium locates the date picker field by its ID and inputs a date using the send_keys() method.

Handling Calendar-Based Date Pickers

Calendar-based date pickers require selecting a date from a pop-up calendar. In these cases, you’ll need to interact with multiple elements, such as the month, year, and date options.


# Click to open the date picker
driver.find_element(By.ID, 'datePicker').click()

# Select the desired month and year, if necessary
driver.find_element(By.XPATH, '//select[@class="ui-datepicker-month"]/option[text()="October"]').click()
driver.find_element(By.XPATH, '//select[@class="ui-datepicker-year"]/option[text()="2024"]').click()

# Select the specific day
driver.find_element(By.XPATH, '//a[text()="22"]').click()

This example demonstrates how to interact with the calendar by selecting the month, year, and day using XPath.

Using JavaScript to Set Date

Some date pickers might be challenging to interact with directly. In such cases, executing JavaScript to set the date is a more efficient approach.


# Execute JavaScript to set the date
driver.execute_script("document.getElementById('datePicker').value = '2024-10-22';")

Here, we use JavaScript to directly modify the value of the date picker field, bypassing the need to interact with the calendar.

Handling Dynamic Date Pickers

If the date picker has dynamic behavior (like AJAX requests for available dates), consider using explicit waits to wait for the date picker elements to load before interacting with them.


from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Wait for the date picker to be clickable
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'datePicker')))

Best Practices

  • Always verify the format of the date required by the date picker (e.g., MM/DD/YYYY).
  • Use implicit waits or explicit waits when handling dynamic date pickers.
  • Consider using JavaScript when direct interaction with a date picker is difficult.

Conclusion

Handling date pickers in Python Selenium is essential for automating web forms. Whether it's a simple text input or a complex calendar, Selenium provides multiple ways to interact with date fields effectively.

For more tips on handling web elements, check out our guide on handling dropdowns.