简体   繁体   中英

Calendar picker with Selenium and Python

I want to click a day of the calendar(doesn't matter what day) if it's available

There are days that are availables and other that they aren't.

The calendar is made with table tag, each td tag, if it isn't available , has a class notSelectableDay .

I have to reload the page till the program found a available day which has the class selectableDay .

The program structure its in this other question i made if else loop on Python. Checking a class name with Selenium

Could this work:

if driver.find_elements_by_class_name("selectableDay"):

    driver.find_element_by_class_name("selectableDay").click()

I`ve made another question more clearly explainded :

I got a calender picker. How to select the available day with Selenium and Python?

Here is the piece of code where you can pick required date from the calendar. Just you need to pass date which you want to be selected from the calendar.

from selenium import webdriver 

def datepicker(date):
    driverInstance = webdriver.Chrome()
    driverInstance.get("http://www.seleniumframework.com/Practiceform/")
    driverInstance.maximize_window()
    driverInstance.find_element_by_id("vfb-8").click()
    elements = driverInstance.find_elements_by_xpath(".//*[@id='ui-datepicker-div']/table/tbody/tr/td/a") 
    for dates in elements:
        if(dates.is_enabled() and dates.is_displayed() and str(dates.get_attribute("innerText")) == date):
            dates.click()

if you want to select date 10 from calendar the pass string "10" to the function

example: datepicker("10")

Let me know if you have any issue.

elementos = driver.find_elements_by_class_name("calendarCellOpen")

   while True:

            if elementos:
                driver.find_element_by_class_name("calendarCellOpen").click()
                driver.find_element_by_id("ctl00_ContentPlaceHolder1_acc_Calendario1_repFasce_ctl01_btnConferma").click() #Confirm button
            else:

                driver.find_element_by_xpath("//input[@value='>']").click() #Forward the calendar
                driver.find_element_by_xpath("//input[@value='<']").click() #Back the calendar
                if elementos:
                    driver.find_element_by_class_name("calendarCellOpen").click()
                    driver.find_element_by_id("ctl00_ContentPlaceHolder1_acc_Calendario1_repFasce_ctl01_btnConferma").click() #Confirm button

Maybe this can work .. instead find_element with xpath but i think it will work anyway..

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