如何让Python中的Selenium使用下拉日历?

时间:2016-04-17 14:47:14

标签: python selenium selenium-webdriver calendar

嘿伙计们,所以我正在尝试构建一个小程序,使用Selenium访问网站:http://www.booking.com/,用户在其中放入目的地,签入/结帐日期。但我不知道如何使用Selenium来处理日历。从我尝试过的逻辑中查看下面的代码。如果您了解问题并且可以解决问题,请有人帮忙。谢谢!

我已经尝试为网站booking.com编写以下内容:登记部分:

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 as EC
from selenium.webdriver.common.keys import Keys
import time

browser = webdriver.Firefox()
browser.get('http://www.booking.com')

wait = WebDriverWait(browser, 2)
D_H_Name = wait.until(EC.visibility_of_element_located((By.ID, "ss")))
D_H_Name.click()
D_H_Name.clear()
print("Enter Destination/Hotel Name:")
D_H_Name.send_keys(input())

time.sleep(3)
D_H_Name.send_keys(Keys.DOWN)

time.sleep(1)
D_H_Name.send_keys(Keys.ENTER)

CheckIn_date = browser.find_element_by_css_selector("div.class ")

#wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.sb-calendar__col")))
#print("Enter CheckIn Date:")
#CheckIn_date.send_keys(input())


CheckIn_date.send_keys(input())

2 个答案:

答案 0 :(得分:0)

您应该可以使用Select功能:

from selenium.webdriver.support.ui import Select
selectDay = Select(driver.find_element_by_name('checkin_monthday'))
selectDay.select_by_visible_text("20")

selectMonth = Select(driver.find_element_by_name('checkin_year_month'))
selectMonth.select_by_visible_text("April 2016")

Select可以处理索引,文本或选项值:

select.select_by_index(index)
select.select_by_visible_text("text")
select.select_by_value(value)

答案 1 :(得分:0)



HI This is kumar.try the following apporach

 WebDriver driver = new firefoxDriver();
 driver.get("http://www.booking.com/");
  driver.findElement(By.xpath("//button[@class='sb-dates__calendar-button b-datepicker b-sprite calendar_icon']")).clcik();
driver.findElement(By.xpath("//span[@class='c2-day-inner'](text()='20')")).clcik();