简体   繁体   中英

Select date from date picker in yatra.Com

Hi I am new to python and Selenium. I am learning it out of interest. My question is I want to select departure date and arrival date in below site. https://www.yatra.com/ I need the code in python using selenium, can any one please help me with this. You can use any other library if required.

Thanks for assistance.

  1. Come up with a relevant locator strategy to identify the date pickers and dates. Use the associated find_element function implementation
  2. Click "origin" datepicker
  3. Use Explicit Wait to wait until the available dates list appears (you need to do it as the dates list as clicking on the datepicker doesn't trigger page reload and if you will attempt to click at the desired date immediately - the date will be not there yet, check out How to use Selenium to test web applications using AJAX technology for more details)
  4. Choose desired date (or go for a random one)
  5. Click the desired date
  6. Again use Explicit Wait for the dates list to disappear
  7. Repeat steps 2-6 for the "destination" datepicker

Example code:

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
import random

driver = webdriver.Chrome("c:\\path\\to\\chromedriver.exe")  
driver.maximize_window()
driver.get('https://www.yatra.com')

wait = WebDriverWait(driver, 10)

departure = wait.until(
    expected_conditions.presence_of_element_located((By.XPATH, "//input[@name='flight_origin_date']"))).click()

wait.until(expected_conditions.visibility_of_any_elements_located((By.XPATH,"//td[@data-date]")))
dates = driver.find_elements_by_xpath("//td[@data-date]")
driver.execute_script("arguments[0].click()",random.choice(dates))
wait.until(expected_conditions.invisibility_of_element_located((By.XPATH,"//td[@data-date]")))
destination = wait.until(
    expected_conditions.presence_of_element_located((By.XPATH, "//input[@name='flight_destination_date']"))).click()

wait.until(expected_conditions.visibility_of_any_elements_located((By.XPATH,"//td[@data-date]")))
dates = driver.find_elements_by_xpath("//td[@data-date]")
random.choice(dates).click()
wait.until(expected_conditions.invisibility_of_element_located((By.XPATH,"//td[@data-date]")))

driver.quit()

The above code assumes Chrome browser and ChromeDriver , however you can use different browser of your choice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM