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

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

This is a calendar html (or you can find it at 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') ) 

What then?

+1
source share
1 answer

First you click on the date picker input:

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

then you expect the calendar to appear:

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

then you click on it some date:

 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 check the calendar using some browser tools, you will see that each "day" element has a td element with the data-year and data-month attributes, 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']

+1
source

Source: https://habr.com/ru/post/1244408/


All Articles