简体   繁体   English

从yatra.com中的日期选择器中选择日期

[英]Select date from date picker in yatra.Com

Hi I am new to python and Selenium. 嗨,我是python和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. https://www.yatra.com/我需要使用selenium的python中的代码,任何人都可以帮助我。 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 使用关联的find_element函数实现
  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) 使用Explicit Wait等待直到出现可用的日期列表(您需要做为日期列表,因为单击日期选择器不会触发页面重新加载,并且如果您尝试立即单击所需的日期-日期不会在那里,请查看如何使用Selenium通过AJAX技术测试Web应用程序以了解更多详细信息)
  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 对“目标”日期选择器重复步骤2-6

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. 上面的代码假定使用Chrome浏览器和ChromeDriver ,但是您可以使用其他选择的浏览器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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