Skip to content
Advertisement

Calendar Data Picker in Selenium

Hi I am trying to figure it out how to pick day from the calendar in Python. I would like to select the date 04/27/2022 under requested loading date. I read some post related to this but I could not figure it out. This is what I did so far but not working.

enter image description here

driver = webdriver.Chrome(PATH)
link = "https://www.freightquote.com/"
driver.get(link) 

sleep(2)
driver.maximize_window()


#click Accept
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='truste-consent-button']"))).click()

#Scroll down:

 total_height = int(driver.execute_script("return document.body.scrollHeight"))
 [enter image description here][1]for i in range(1, 500, 5):
    driver.execute_script("window.scrollTo(0, {});".format(i))

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//input[@class="datepicker-here hasDatepicker-od" and @id="txtShippingDate" ]'))).click()

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//div[@class="datepicker--cell datepicker--cell-day" and @data-date="27" and @data-month="3" an @data-year="2022"]'))).click()

Advertisement

Answer

You do not really have to click on calendar and select a value, you can just execute the JS command like below:

driver.maximize_window()
wait = WebDriverWait(driver, 30)

driver.get("https://www.freightquote.com/")
try:
    wait.until(EC.element_to_be_clickable((By.ID, "truste-consent-button"))).click()
    print("Clicked on cookies accept button")
except:
    pass

time.sleep(5)
requested_loading_date = wait.until(EC.visibility_of_element_located((By.ID, "txtShippingDate")))
driver.execute_script("arguments[0].value = arguments[1]", requested_loading_date, "04/27/2022")

Imports:

import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement