Selenium: Select dates from a calendar

Viewed 164

I have been trying to automate checking hotel prices via the website www.trivago.ie I am struggling to select the dates from the calendar which pops up after you select a hotel.

I can't seem to get the correct class to allow me to select and update both the start and end date. I have attached my basic code below. Could someone please help me extend it such that I can select the dates after I have selected the hotel.

from selenium.webdriver.chrome.options import Options
from selenium import webdriver
import time
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_argument('window-size=2560,1440')
chrome_options = Options()

driver = webdriver.Chrome('*/drivers/chromedriver/win32/91.0.4472.101/chromedriver', chrome_options=options)

# Open the website page
driver.get('https://www.trivago.ie')

# Enter the hotel into the search box
driver.find_element_by_id("querytext").send_keys("test hotel")

# Allow time for options to populate
time.sleep(1)

# Select the first suggestion
driver.find_elements_by_class_name("ssg-suggestion__info")[0].click()

# Allow time for the calander to pop-up
time.sleep(3)

# Try, but fail, to select the date
#driver.find_elements_by_class_name("cal-day cal-is-weekend cal-is-selectable")[0].click()
driver.find_element_by_tag_name("cal-heading-month cal-heading-day4").click()

Any help would be much appreciated, I have hit quite the road-block. Thank you.

2 Answers

If you are willing to select the first available date (and default to a one day stay), there is a class for the first available date. So the following is working for me:

# Try, but fail, to select the date
#driver.find_elements_by_class_name("cal-day cal-is-weekend cal-is-selectable")[0].click()
driver.find_element_by_class_name("cal-is-range-start").click()
time.sleep(1)#time for end date to populate
driver.find_element_by_css_selector('.search-button__label').click()

1 There is "Accept cookies" button. You have to click it, otherwise you will not be able to click dates correctly.

2 You need to wait till the first date is clickable and click it afterwards.

3 from selenium.webdriver import ActionChains - Import ActionChains for selecting a second date. Selenium will move to it and click the date.

4 Make sure the locators you are using are unique. Check the ones from my solution.

Note that I used time.sleep(3) to wait for cookies OK button. element_to_be_clickable did not work because the button is moving. Here is the field for improvements.

SOLUTION

from selenium.webdriver.chrome.options import Options
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver import ActionChains

options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_argument('window-size=2560,1440')
chrome_options = Options()

driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver', chrome_options=options)

# Open the website page
driver.get('https://www.trivago.ie')
wait = WebDriverWait(driver, 10)

# Accept cookies
time.sleep(3)
accept_cookies = driver.find_element_by_css_selector("#onetrust-button-group>#onetrust-accept-btn-handler")
driver.execute_script("arguments[0].click();", accept_cookies)

# Enter the hotel into the search box
driver.find_element_by_id("querytext").send_keys("test hotel")

# Allow time for options to populate
time.sleep(1)

# Select the first suggestion
driver.find_elements_by_class_name("ssg-suggestion__info")[0].click()


# Allow the time for the first date to become clickable and click it

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "time[datetime='2021-07-01']")))
driver.find_element_by_css_selector("time[datetime='2021-07-11']").click()

#  Move to the next date and click it
second_date = driver.find_element_by_css_selector("time[datetime='2021-07-15']")
actions = ActionChains(driver)
actions.move_to_element(second_date)
actions.click().perform()

Result (selected dates):

enter image description here

Related