我有一个日历选择器.如何使用 Selenium 和 Python 选择可用日期? [英] I got a calender picker. How to select the available day with Selenium and Python?

查看:33
本文介绍了我有一个日历选择器.如何使用 Selenium 和 Python 选择可用日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可用天数有一个 .calendarCellOpen 类:

table.calendario .calendarCellOpen 输入 {}

这是日历css:

 #calwrapper{最小高度:230px;边距顶部:10px;}#日历{向左飘浮;左边距:15px;/*丹尼尔10-04-2014*/}跨度日历{显示:块;保证金:0;}table.fasce{左边距:20px;}台面{背景图片:url('../images/tab_body.png');背景重复:重复-x;字体大小:12px;}table.fasce tr{边框底部:#f5f4e7 细虚线;}table.calendario{边框顶部:0px !重要;}table.calendario, table.fasce{宽度:300px;背景颜色:白色!重要;字体大小:15px;右边框:#f5f4e7 1px 实心!重要;左边框:#f5f4e7 1px 实心!重要;边框底部:#f5f4e7 1px 实心!重要;}table.calendario td,table.fasce td{文字对齐:居中;}table.calendario .calTitolo{背景图片:url('../images/tab_body.png');背景重复:重复-x;边距:0px!重要;填充:0px!重要;字体大小:12px;}table.calendario .calTitolo td{填充:0px 5px 0px 5px;宽度:14.3%;}table.calendario .calDayHeader/* 里加 */{背景色:#FCFBF7;字体大小:12px;}table.calendario .otherMonthDay{颜色:#C0C0C0;}table.calendario .cellaSelezionata/* CELLA */{背景色:#EDEBD5 !重要;边界折叠:折叠!重要;字体粗细:粗体;}table.calendario .calendarCellOpen 输入{颜色:#208020 !重要;/*高可用(绿色)*/字体粗细:粗体;}table.calendario .calendarCellRed{颜色:红色!重要;/*无可用性*/字体粗细:粗体;}table.calendario .calendarCellMed 输入{颜色:#F09643 !重要;/*Disponibilità媒体*/字体粗细:粗体;}.pulsanteCalendario{边框:0px;背景色:透明;光标:指针;填充:0px 0px 0px 0px;边距:0px;高度:20px;宽度:100%;溢出:可见;文字对齐:居中;字体大小:16px;}.pulsanteCalendario:悬停{文字装饰:下划线;}#传奇{底边距:8px;宽度:100%;}#legend ul{列表样式类型:无;}#legend ul li{显示:内联;左边距:20px;}

问题是我想选择(用 Selenium 点击它)可用的日期(哪一天无关紧要).只是任何看起来可用的日子(绿色).

这是日历:

 elementos = driver.find_elements_by_class_name("calendarCellOpen")为真:如果元素:driver.find_element_by_class_name("calendarCellOpen").click()driver.find_element_by_id("ctl00_ContentPlaceHolder1_acc_Calendario1_repFasce_ctl01_btnConferma").click() #确认按钮别的:driver.find_element_by_xpath("//input[@value='<']").click() #back如果元素:driver.find_element_by_class_name("calendarCellOpen").click()driver.find_element_by_id("ctl00_ContentPlaceHolder1_acc_Calendario1_repFasce_ctl01_btnConferma").click()driver.find_element_by_xpath("//input[@value='>']").click() #forward如果元素:driver.find_element_by_class_name("calendarCellOpen").click()driver.find_element_by_id("ctl00_ContentPlaceHolder1_acc_Calendario1_repFasce_ctl01_btnConferma").click()

这是我制作的一些代码

我来来回回,因为这是重新加载日历的唯一方法..

这是日历的 HTML:

<div id="legend" style="padding-left:15px; margin-bottom:20px"><table style="width:90%; border-collapse:collapse; border: 0px"><tr style="line-height:15px"><td style="background-color:Red; width:80px; margin-right:10px"></td><td style="width: 383px; padding-left:5px">Tutto occupato # all none available</td><td style="background-color:#F09643; width:80px"></td><td style="width: 450px; padding-left:5px">媒体disponibilit&agrave;#半可用</td><td style="background-color:#058d08; width:80px"></td><td style="width: 383px; padding-left:5px">Posti disponibili #available</td><td style="background-color:#000000; width:80px"></td><td style="width: 383px; padding-left:5px">Non disponibile # 无可用</td></tr>

<div id="日历"><span id="ctl00_ContentPlaceHolder1_acc_Calendario1_myCalendario1"class="日历"><table class="calendario" summary="Summary" cellpacing="0"><caption>Calendario eventi</caption><th><输入类型=提交"name="ctl00$ContentPlaceHolder1$acc_Calendario1$myCalendario1$ctl01"价值="&lt;"title="Clicca qui per andare al mese precedente"class="pulsanteCalendario"/></th><th colspan="5"><span>agosto, 2017</span></th><th><输入类型=提交"name="ctl00$ContentPlaceHolder1$acc_Calendario1$myCalendario1$ctl03"值=">"title="Clicca qui per andare al mese successivo"class="pulsanteCalendario"/></th></tr><tr>lun<th class="calDayHeader"scope="col">marmer<th class="calDayHeader" scope="col">gio</th><th class="calDayHeader" scope="col">ven</th><th class="calDayHeader" scope="col">sab</th>dom</tr><tr><td title="Giorno non disponibile" class="otherMonthDay">31</td><td title="Tutto occupato" class="calendarCellRed">1</td><td title="Giorno non disponibile" class="noSelectableDay">2</td><td title="Tutto occupato" class="calendarCellRed">3</td><td title="Tutto occupato" class="calendarCellRed">4</td><td title="Giorno non disponibile" class="noSelectableDay">5</td><td title="Giorno non disponibile" class="noSelectableDay">6</td></tr><tr><td title="Tutto occupato" class="calendarCellRed">7</td><td class="calendarCellOpen"><输入类型=提交"name="ctl00$ContentPlaceHolder1$acc_Calendario1$myCalendario1$ctl12"value="8" title="8 agosto 2017, Posti disponibili"class="pulsanteCalendario"/></td><td class="calendarCellOpen"><输入类型=提交"name="ctl00$ContentPlaceHolder1$acc_Calendario1$myCalendario1$ctl12"value="8" title="8 agosto 2017, Posti disponibili"class="pulsanteCalendario"/></td><td class="calendarCellOpen"><输入类型=提交"name="ctl00$ContentPlaceHolder1$acc_Calendario1$myCalendario1$ctl12"value="8" title="8 agosto 2017, Posti disponibili"class="pulsanteCalendario"/></td><td class="calendarCellOpen"><输入类型=提交"name="ctl00$ContentPlaceHolder1$acc_Calendario1$myCalendario1$ctl12"value="8" title="8 agosto 2017, Posti disponibili"class="pulsanteCalendario"/></td><td title="Giorno non disponibile" class="noSelectableDay">9</td><td title="Giorno non disponibile" class="noSelectableDay">10</td></tr><tr><td title="Giorno non disponibile" class="noSelectableDay">14</td><td title="Giorno non disponibile" class="noSelectableDay">15</td><td title="Giorno non disponibile" class="noSelectableDay">16</td><td title="Giorno non disponibile" class="noSelectableDay">17</td><td title="Giorno non disponibile" class="noSelectableDay">18</td><td title="Giorno non disponibile" class="noSelectableDay">19</td><td title="Giorno non disponibile" class="noSelectableDay">20</td></tr><tr><td title="Giorno non disponibile" class="noSelectableDay">21</td><td title="Giorno non disponibile" class="noSelectableDay">22</td><td title="Giorno non disponibile" class="noSelectableDay">23</td><td title="Giorno non disponibile" class="noSelectableDay">24</td><td title="Giorno non disponibile" class="noSelectableDay">25</td><td title="Giorno non disponibile" class="noSelectableDay">26</td><td title="Giorno non disponibile" class="noSelectableDay">27</td></tr><tr><td title="Giorno non disponibile" class="noSelectableDay">28</td><td title="Giorno non disponibile" class="noSelectableDay">29</td><td title="Giorno non disponibile" class="noSelectableDay">30</td><td title="Giorno non disponibile" class="noSelectableDay">31</td><td title="Giorno non disponibile" class="otherMonthDay">1</td><td title="Giorno non disponibile" class="otherMonthDay">2</td><td title="Giorno non disponibile" class="otherMonthDay">3</td></tr></table></span>

<div id="orari" ><输入类型=隐藏"name="ctl00$ContentPlaceHolder1$acc_Calendario1$HiddenField1"id="ctl00_ContentPlaceHolder1_acc_Calendario1_HiddenField1"/>

解决方案

这是我要做的,但我不太确定这会起作用:

 为真:对于 elementos 中的日期:如果dates.is_enabled():日期.点击()driver.find_element_by_id("ctl00_ContentPlaceHolder1_acc_Calendario1_repFasce_ctl01_btnConferma").click()#if 元素 >0:#driver.find_element_by_class_name("calendarCellOpen").click()#别的:driver.find_element_by_xpath("//input[@value='<']").click()driver.find_element_by_xpath("//input[@value='>']").click()

the available days has a class .calendarCellOpen:

table.calendario .calendarCellOpen input {

}

Here it is the calendar css:

    #calwrapper
    {
    min-height:230px;  
    margin-top:10px;  
    }


    #calendar
    {
    float:left;
    margin-left: 15px; /*Daniele 10-04-2014*/
    }


span.calendario
{
    display:block;
    margin:0;
}

table.fasce
{
    margin-left:20px;
}

table.fasce th
{
    background-image: url( '../images/tab_body.png' );
    background-repeat: repeat-x;
    font-size:12px;
}

table.fasce tr
{
    border-bottom: #f5f4e7 thin dotted;
}

table.calendario
{
    border-top: 0px !important;
}

table.calendario, table.fasce
{
    width: 300px;
    background-color: White !important;
    font-size: 15px;
    border-right: #f5f4e7 1px solid !important;
    border-left: #f5f4e7 1px solid !important;
    border-bottom: #f5f4e7 1px solid !important;
}

table.calendario td, table.fasce td
{
    text-align:center;
}

table.calendario .calTitolo
{
    background-image: url( '../images/tab_body.png' );
    background-repeat: repeat-x;
    margin: 0px !important;
    padding: 0px !important;
    font-size:12px;
}

table.calendario .calTitolo td
{
    padding:0px 5px 0px 5px;
    width:14.3%;
}

table.calendario .calDayHeader /* RIGA */
{   
    background-color:#FCFBF7;
    font-size:12px;
}

table.calendario .otherMonthDay
{
    color: #C0C0C0;
}

table.calendario .cellaSelezionata /* CELLA */
{   
    background-color:#EDEBD5 !important;    
    border-collapse:collapse !important;
    font-weight:bold;   
}

table.calendario .calendarCellOpen input
{
    color:#208020 !important;   /*High availability (green)*/
    font-weight:bold;       
}

table.calendario .calendarCellRed
{
    color:Red !important;   /*noe availability*/ 
    font-weight:bold;   
}

table.calendario .calendarCellMed input
{
    color:#F09643 !important;   /*Disponibilità media*/
    font-weight:bold;   
}


.pulsanteCalendario
{    
    border: 0px;
    background-color: Transparent;
    cursor: pointer;
    padding: 0px 0px 0px 0px;
    margin: 0px;  
    height:20px;
    width:100%;
    overflow:visible; 
    text-align:center;
    font-size:16px;
}

.pulsanteCalendario:hover
{
    text-decoration:underline;
}

#legend
{
    margin-bottom:8px;
    width:100%;    
}

#legend ul
{
    list-style-type:none;
}

   #legend ul li
   {
    display:inline;
    margin-left:20px;
   }

The thing is that i want to select (clicking on it with Selenium) the day available(doesn`t matter which day).Just any day which appears to be available(green).

Here is the calendar:

 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()  #back
                if elementos:
                    driver.find_element_by_class_name("calendarCellOpen").click()
                    driver.find_element_by_id("ctl00_ContentPlaceHolder1_acc_Calendario1_repFasce_ctl01_btnConferma").click()

                driver.find_element_by_xpath("//input[@value='>']").click() #forward
                if elementos:
                    driver.find_element_by_class_name("calendarCellOpen").click()
                    driver.find_element_by_id("ctl00_ContentPlaceHolder1_acc_Calendario1_repFasce_ctl01_btnConferma").click()

This some code i made

I made back and foward because is th only way to reload the calendar..

This is the HTML of the calendar:

<div id="calwrapper">
<div id="legend" style="padding-left:15px; margin-bottom:20px">


    <table style="width:90%; border-collapse:collapse; border: 0px">
    <tr style="line-height:15px">
        <td style="background-color:Red; width:80px; margin-right:10px">
        </td>

        <td style="width: 383px; padding-left:5px">
            Tutto occupato # all none available
        </td>

        <td style="background-color:#F09643; width:80px">
        </td>

        <td style="width: 450px; padding-left:5px">
            Media disponibilit&agrave; #half available
        </td>

        <td style="background-color:#058d08; width:80px">
        </td>

        <td style="width: 383px; padding-left:5px">
            Posti disponibili  #available
        </td>

        <td style="background-color:#000000; width:80px">
        </td>

        <td style="width: 383px; padding-left:5px">
            Non disponibile # none available
        </td>
    </tr>
</table>
</div>
<div id="calendar">
    <span id="ctl00_ContentPlaceHolder1_acc_Calendario1_myCalendario1" 
class="calendario">
<table class="calendario" summary="Summary" cellspacing="0">
<caption>Calendario eventi</caption>
<tr class="calTitolo">
<th>
<input type="submit" 
name="ctl00$ContentPlaceHolder1$acc_Calendario1$myCalendario1$ctl01" 
value="&lt;" title="Clicca qui per andare al mese precedente" 
 class="pulsanteCalendario" />
</th>
<th colspan="5">
<span>agosto, 2017</span>
</th>
<th>
<input type="submit" 
name="ctl00$ContentPlaceHolder1$acc_Calendario1$myCalendario1$ctl03" 
value=">" title="Clicca qui per andare al mese successivo" 
 class="pulsanteCalendario" />
</th>
</tr>
<tr>
<th class="calDayHeader" scope="col">lun</th>
<th class="calDayHeader" 
scope="col">mar</th>
<th class="calDayHeader" scope="col">mer</th>
<th class="calDayHeader" scope="col">gio</th>
<th class="calDayHeader" scope="col">ven</th>
<th class="calDayHeader" scope="col">sab</th>
<th class="calDayHeader" scope="col">dom</th>
</tr>
<tr>
<td title="Giorno non disponibile" class="otherMonthDay">31</td>
<td  title="Tutto occupato" class="calendarCellRed">1</td>
<td title="Giorno non disponibile" class="noSelectableDay">2</td>
<td title="Tutto occupato" class="calendarCellRed">3</td>
<td title="Tutto occupato" class="calendarCellRed">4</td>
<td title="Giorno non disponibile" class="noSelectableDay">5</td>
<td title="Giorno non disponibile" class="noSelectableDay">6</td>
</tr>
<tr>
<td title="Tutto occupato" class="calendarCellRed">7</td>
<td class="calendarCellOpen">
<input type="submit" 
name="ctl00$ContentPlaceHolder1$acc_Calendario1$myCalendario1$ctl12" 
value="8" title="8 agosto 2017, Posti disponibili" 
class="pulsanteCalendario" />
</td>
<td class="calendarCellOpen">
<input type="submit" 
name="ctl00$ContentPlaceHolder1$acc_Calendario1$myCalendario1$ctl12" 
value="8" title="8 agosto 2017, Posti disponibili" 
class="pulsanteCalendario" />
</td>
<td class="calendarCellOpen">
<input type="submit" 
name="ctl00$ContentPlaceHolder1$acc_Calendario1$myCalendario1$ctl12" 
value="8" title="8 agosto 2017, Posti disponibili" 
class="pulsanteCalendario" />
</td>
<td class="calendarCellOpen">
<input type="submit" 
name="ctl00$ContentPlaceHolder1$acc_Calendario1$myCalendario1$ctl12" 
value="8" title="8 agosto 2017, Posti disponibili" 
class="pulsanteCalendario" />
</td>
<td title="Giorno non disponibile" class="noSelectableDay">9</td>
<td title="Giorno non disponibile" class="noSelectableDay">10</td>

</tr><tr>
<td title="Giorno non disponibile" class="noSelectableDay">14</td>
<td title="Giorno non disponibile" class="noSelectableDay">15</td>
<td title="Giorno non disponibile" class="noSelectableDay">16</td>
<td title="Giorno non disponibile" class="noSelectableDay">17</td>
<td title="Giorno non disponibile" class="noSelectableDay">18</td>
<td title="Giorno non disponibile" class="noSelectableDay">19</td>
<td title="Giorno non disponibile" class="noSelectableDay">20</td>
</tr><tr>
<td title="Giorno non disponibile" class="noSelectableDay">21</td>
<td title="Giorno non disponibile" class="noSelectableDay">22</td>
<td title="Giorno non disponibile" class="noSelectableDay">23</td>
<td title="Giorno non disponibile" class="noSelectableDay">24</td>
<td title="Giorno non disponibile" class="noSelectableDay">25</td>
<td title="Giorno non disponibile" class="noSelectableDay">26</td>
<td title="Giorno non disponibile" class="noSelectableDay">27</td>
</tr><tr>
<td title="Giorno non disponibile" class="noSelectableDay">28</td>
<td title="Giorno non disponibile" class="noSelectableDay">29</td>
<td title="Giorno non disponibile" class="noSelectableDay">30</td>
<td title="Giorno non disponibile" class="noSelectableDay">31</td>
<td title="Giorno non disponibile" class="otherMonthDay">1</td>
<td title="Giorno non disponibile" class="otherMonthDay">2</td>
<td title="Giorno non disponibile" class="otherMonthDay">3</td>
</tr></table></span>
</div>
<div id="orari" >
    <input type="hidden" 
 name="ctl00$ContentPlaceHolder1$acc_Calendario1$HiddenField1" 
 id="ctl00_ContentPlaceHolder1_acc_Calendario1_HiddenField1" />

</div>
</div>

解决方案

This is what i gain to do, but im not quite sure that this is going to work:

            while True:
            for dates in elementos:
                if dates.is_enabled():
                    dates.click()
                    driver.find_element_by_id("ctl00_ContentPlaceHolder1_acc_Calendario1_repFasce_ctl01_btnConferma").click()
            #if elementos > 0:
                #driver.find_element_by_class_name("calendarCellOpen").click()

                #else:

            driver.find_element_by_xpath("//input[@value='<']").click()
            driver.find_element_by_xpath("//input[@value='>']").click()

这篇关于我有一个日历选择器.如何使用 Selenium 和 Python 选择可用日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆