How to Select date from a datepicker with Selenium Webdriver using Python?


We can select a date from a datepicker with Selenium webdriver using Python. To identify a particular date, first we have to use the find_elements method and identify all the dates having a common locator value.

The find_elements returns a list of matching elements. We have to iterate through this list and search for the date which meets our criteria. Once we get that date, we would select it. Then break out from this iteration.

Example

from selenium import webdriver
#set chromodriver.exe path
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
driver.implicitly_wait(0.5)
#launch URL
driver.get("https://jqueryui.com/datepicker/")
#switch to frame
l = driver.find_element_by_xpath("//iframe[@class='demo-frame']")
driver.switch_to.frame(l);
#identify element inside frame
d= driver.find_element_by_id("datepicker")
d.click()
#identify list of all dates
m = driver.find_elements_by_xpath("//table/tbody/tr/td")
#iterate over list
for i in m:
#verify required date then click
   if i.text == '3':
      i.click()
   break
#get selected date
s = d.get_attribute('value')
print("Date entered is: ")
print(s)
#browser quit
driver.quit()

Output

Updated on: 08-Apr-2021

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements