简体   繁体   中英

How to select a specific date from a calender, using python-selenium?

For example, if I want to use python-selenium to select a specific check-in and check-out date in www.priceline.com, what should I do?

This is the calender html (or you can find it on www.priceline.com)

<input name="checkInDate" pclnfield="ts" pclnoptional="true" preformat="" pclnprepop="false" 
pclnfocusnext="hotel-checkout" type="text" id="hotel-checkin" placeholder="Choose Date" 
autocomplete="off" class="hasDatepicker">

This is my code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
url = 'http://www.priceline.com/' #
driver = webdriver.Firefox()
driver.get(url)
sleep(3)
select = Select(  driver.find_element_by_id('hotel-checkin')  )

Then what?

First you click at date-picker input:

driver.find_element_by_xpath("//input[@id='hotel-checkin'").click()

then you wait for calendar to appear:

wait.until(lambda driver: driver.find_element_by_xpath("//div[@id='ui-datepicker-div']"))

then you click some date within it:

driver.find_element_by_xpath("//div[@id='ui-datepicker-div']//a[@class='ui-state-default'][text()='HERE_IS_DATE_LIKE_10']")).click() # meaning //div[@id='ui-datepicker-div']//td/a[@class='ui-state-default'][text()='10']

something like that. If you inspect calendar with some browser dev tools, you'll see that each 'day' element is td element with attributes data-year and data-month and you can play around them. Like //div[@id='ui-datepicker-div']//td[@data-year='2014'][@data-month='8']/a[@class='ui-state-default'][text()='10']

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