Nichas
Nichas

Reputation: 23

How to select the next day from the calendar using Selenium-Python

Unable to select tomorrow's date in the calendar using Selenium

I have tried using find_element_by_css_selector and find_element_by_xpath but nothing seems to be working.

import time
import datetime
from time import strftime
from selenium import webdriver
browser = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\Application\chromedriver_win32\chromedriver.exe")
browser.get('https://www.****.com')

browser.find_element_by_id("UserName").send_keys("***")
browser.find_element_by_id("password").send_keys("***")
browser.find_element_by_id("submit-sign-in").click()


browser.find_element_by_id("Information").click()
browser.find_element_by_id("Amenities").click()

browser.find_element_by_id("reserve").click()
browser.find_element_by_id("resv-date").click()

******#The code works fine until here**********

#Trying to pick tomorrows date from the calendar.
browser.find_element_by_css_selector("a.ui-state-default.ui-state-active").click()

Not getting an error message but it doesn't select the required date

I have attached the image below:

<td class="  ui-datepicker-today" data-handler="selectDay" data-event="click" data-month="4" data-year="2019"><a class="ui-state-default ui-state-highlight" href="#">17</a></td>
<a class="ui-state-default ui-state-highlight" href="#">17</a></td>
<td class=" ui-datepicker-week-end  ui-datepicker-current-day" data-handler="selectDay" data-event="click" data-month="4" data-year="2019"><a class="ui-state-default ui-state-active" href="#">18</a></td>
<a class="ui-state-default ui-state-active" href="#">18</a>

Snippet Calendar Date picker

Upvotes: 2

Views: 1727

Answers (1)

supputuri
supputuri

Reputation: 14135

You have to use the below css to select the next day.

td.ui-datepicker-current-day + td

Code:

browser.find_element_by_css_selector("td.ui-datepicker-current-day+td").click()

+ the the adjacent sibling combinator. If you consider in the current example td.ui-datepicker-current-day is selecting the current day i.e 17 but we want to select the following sibling so using + will select the sibling and we are specifying filtering the adjacent sibling to be td.

If you want to use xpath for the same case then you can use following-sibling and filter by tag nametd as shown below.

//td[contains(@class, 'ui-datepicker-current-day')]/following-sibling::td

In nutshell + in css is almost equivalent to following-sibling in xpath, which will help selecting the sibling items.

Refer to the below links on CSS and Xpath on the method discussed above.

CSS

Upvotes: 1

Related Questions