无法使用 python3.7 和 selenium 使用 FirefoxWebdriver 从 datepicker select 日期

Cannot select date from datepicker with python3.7 and selenium using the FirefoxWebdriver

我正在尝试自动从日期选择器中选择预定义日期。该日期选择器是一个更大的弹出窗口中的弹出窗口。一个也算不出来。

我创建此功能时等待了很多次,因为网站速度很慢

下面是日期选择器的html

<div class="ui-datepicker-header ui-widget-header ui-helper-clearfix ui-corner-all">
    <div class="ui-datepicker-close-button" onclick="CloseDatePicker();"></div><a class="ui-datepicker-next ui-corner-all" onclick="DP_jQuery.datepicker._adjustDate('#startDatePicker', +1, 'M');" title="הבא>"><span class="ui-icon ui-icon-circle-triangle-w">הבא&gt;</span></a><a class="ui-datepicker-prev ui-corner-all ui-state-disabled"
        title="<הקודם"><span class="ui-icon ui-icon-circle-triangle-e">&lt;הקודם</span></a>
    <div class="ui-datepicker-title"><span class="ui-datepicker-month">אוגוסט</span> <span class="ui-datepicker-year">2019</span></div>
</div>
<table class="ui-datepicker-calendar" cellspacing="0" cellpadding="0">
    <thead>
        <tr>
            <th class="ui-datepicker-week-end"><span title="ראשון">א'</span></th>
            <th><span title="שני">ב'</span></th>
            <th><span title="שלישי">ג'</span></th>
            <th><span title="רביעי">ד'</span></th>
            <th><span title="חמישי">ה'</span></th>
            <th><span title="שישי">ו'</span></th>
            <th class="ui-datepicker-week-end"><span title="שבת">שבת</span></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class=" ui-datepicker-week-end ui-datepicker-other-month ui-datepicker-unselectable ui-state-disabled">&nbsp;</td>
            <td class=" ui-datepicker-other-month ui-datepicker-unselectable ui-state-disabled">&nbsp;</td>
            <td class=" ui-datepicker-other-month ui-datepicker-unselectable ui-state-disabled">&nbsp;</td>
            <td class=" ui-datepicker-other-month ui-datepicker-unselectable ui-state-disabled">&nbsp;</td>
            <td class=" ui-datepicker-unselectable ui-state-disabled "><span class="ui-state-default">1</span></td>
            <td class=" ui-datepicker-unselectable ui-state-disabled "><span class="ui-state-default">2</span></td>
            <td class=" ui-datepicker-week-end ui-datepicker-unselectable ui-state-disabled "><span class="ui-state-default">3</span></td>
        </tr>
        <tr>
            <td class=" ui-datepicker-week-end ui-datepicker-days-cell-over  ui-datepicker-today" onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default ui-state-highlight" href="#">4</a></td>
            <td class=" " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">5</a></td>
            <td class=" " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">6</a></td>
            <td class=" " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">7</a></td>
            <td class=" " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">8</a></td>
            <td class=" " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">9</a></td>
            <td class=" ui-datepicker-week-end " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">10</a></td>
        </tr>
        <tr>
            <td class=" ui-datepicker-week-end " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">11</a></td>
            <td class=" " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">12</a></td>
            <td class=" " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">13</a></td>
            <td class=" " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">14</a></td>
            <td class=" " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">15</a></td>
            <td class=" " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">16</a></td>
            <td class=" ui-datepicker-week-end " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">17</a></td>
        </tr>
        <tr>
            <td class=" ui-datepicker-week-end " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">18</a></td>
            <td class=" " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">19</a></td>
            <td class=" " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">20</a></td>
            <td class=" " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">21</a></td>
            <td class=" " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">22</a></td>
            <td class=" " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">23</a></td>
            <td class=" ui-datepicker-week-end " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">24</a></td>
        </tr>
        <tr>
            <td class=" ui-datepicker-week-end " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">25</a></td>
            <td class=" " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">26</a></td>
            <td class=" " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">27</a></td>
            <td class=" " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">28</a></td>
            <td class=" " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">29</a></td>
            <td class=" " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">30</a></td>
            <td class=" ui-datepicker-week-end " onclick="DP_jQuery.datepicker._selectDay('#startDatePicker',7,2019, this);return false;"><a class="ui-state-default" href="#">31</a></td>
        </tr>
    </tbody>
</table>

代码如下

def for_future_date(self):
        driver = self.driver
        WebDriverWait(driver, delay).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#SHOW_CALNDER > div:nth-child(2) > a:nth-child(1)')))
        WebDriverWait(driver, delay)
        while start_date == 1:
            confirm = driver.find_element_by_xpath('//*[@id="approve_checkbox"]')
            confirm.click()
        else:  #package starting not today
            WebDriverWait(driver, delay)
            WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH, '//*[@id="SHOW_CALNDER"]')))
            WebDriverWait(driver, delay)
            WebDriverWait(driver, delay).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="SHOW_CALNDER"]'))).click()
            WebDriverWait(driver, delay)
            #WebDriverWait(driver, delay).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="SHOW_CALNDER"]'))).click()
            WebDriverWait(driver, delay)

            print("opened date picked")
            #WebDriverWait(driver, delay).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[2]/div[1]/div/div[1]/div[3]/strong/div[1]/div[2]/a'))).click()
            driver.find_element_by_class_name('ui-datepicker-trigger').click()

            selecting_all_dates = driver.find_element_by_class_name('ui-state-default') # this selects the class of the table which in turn has all the dates
            date_element = driver.find_element_by_name('22')

            date_element.click()

'find_element_by_name(foo)' 将 select 具有 name="foo"

的元素

您想要查找 "text" 与您的搜索相匹配的元素 driver.find_elements_by_xpath("a[@id='ui-state-default'][text() = '22']")

打开日历后,您只需要下面的定位器。

"//table[@class='ui-datepicker-calendar']//a[.='22']"

可以进一步修改它以接受任何一天,这样您就可以传入一个参数,day,其中包含“22”(或任何所需的日期)并且可以重复使用。

"//table[@class='ui-datepicker-calendar']//a[.='" + day + "']"

一些额外的反馈...

  1. 您可以声明一个等待并重用它。

    wait = WebDriverWait(driver, delay)
    wait.until(...)
    wait.until(...)
    
  2. 如果仅使用 ID 定位元素,请使用 .find_element_by_id('approve_checkbox') 而不是 XPath。

  3. 您无需等待状态然后可点击。除非元素存在,否则无法点击该元素,因此只需等待可点击即可。