带有Selenium和Python的日历选择器

2024-09-30 08:32:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我想单击日历中的某一天(不管哪一天)是否可用

有些日子是可以利用的,而有些日子是没有的

日历是用table标记制作的,每个td标记如果不可用,都有一个类notSelectableDay。在

我必须重新加载页面,直到程序找到一个可用的日期,其中包含类selectableDay。在

程序结构是在我提出的另一个问题if else loop on Python. Checking a class name with Selenium

这是否可行:

if driver.find_elements_by_class_name("selectableDay"):

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

我提出了另一个更明确的问题:

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


Tags: name标记利用byifdriverseleniumwith
2条回答
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

也许这能起作用。。相反,用xpath找到_元素,但我认为它还是可以工作的。。在

下面是一段代码,您可以从日历中选择所需的日期。只需通过您想从日历中选择的日期。在

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()

如果要从日历中选择日期10,请将字符串“10”传递给函数

示例:datepicker("10")

如果你有什么问题请告诉我。在

相关问题 更多 >

    热门问题