0

I want to select date from daterangepicker:

Ex of daterangepicker:

Default: Mar 09, 2022 - Mar 13, 2022

daterangepicker

.html Code:

<input type="text" class="form-control travel-date-section" readonly="" name="daterangepicker" placeholder="Select date range">
    <tr>
    <td class="weekend off ends off disabled" data-title="r0c0">27</td>
    <td class="off ends off disabled" data-title="r0c1">28</td>
    <td class="off disabled" data-title="r0c2">1</td>
    <td class="off disabled" data-title="r0c3">2</td>
    <td class="off disabled" data-title="r0c4">3</td>
    <td class="off disabled" data-title="r0c5">4</td>
    <td class="weekend off disabled" data-title="r0c6">5</td>
    </tr>
<tr>
    <td class="weekend off disabled" data-title="r1c0">6</td>
    <td class="today available" data-title="r1c1">7</td>
    <td class="available" data-title="r1c2">8</td>
    <td class="active start-date available" data-title="r1c3">9</td>
    <td class="available in-range" data-title="r1c4">10</td>
    <td class="available in-range" data-title="r1c5">11</td>
    <td class="weekend available in-range" data-title="r1c6">12</td></tr>
<tr>
</tbody>

After i click daterangepicker. I tried select date '17' by using xpath, css selector and class name but nothing work.

Ex:

#1
element = driver.find_element_by_class_name('active start-date available')
element.send_keys(17)

#2
element = driver.find_element_by_css_selector('.daterangepicker .drp-calendar td.active.start-date')
time.sleep(2)
element.send_keys(17)

#3
element = driver.find_elements_by_xpath('/html/body/div[6]/div[2]/div[1]/table/tbody/tr[2]/td[4]')
time.sleep(2)
element.send_keys(17)

Any kind of help please?

  • What exactly do not work? What exception appears? Can you share a link to the page you are working on? – Prophet Mar 07 '22 at 11:12
  • @Prophet I can't share link because it need username and password to login, i mean by not work not affect the result of daterangepicker. When i use find_element_by_class_name i got no such element: Unable to locate element: {"method":"css selector","selector":".active start-date available"} (Session info: chrome=98.0.4758.102). When i use find_element_by_css_selector i got Message: element not interactable – Samer Adnan Mar 07 '22 at 11:16

2 Answers2

1

I've got a brute-force solution:

from selenium.webdriver.common.action_chains import ActionChains as AC

element = driver.find_element_by_xpath('//td[contains(text(), "9") and contains(@class, "available")]')

AC.move_to_element_with_offset(element, 35, 30).click().perform()

So basically it finds the calendar day '9' and moves 35 pixels right and 30 pixels down, where '17' should be, and clicks there. Obviously this setup will be different for different months. It's a primitive solution but hey, it might just work.

Alichino
  • 1,668
  • 2
  • 16
  • 25
0

Instead of selecting any specific date you can send the date as a character sequence removing the readonly and inducing WebDriverWait for the for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    driver.execute_script("arguments[0].removeAttribute('readonly')", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.form-control.travel-date-section[name='daterangepicker']"))))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.form-control.travel-date-section[name='daterangepicker']"))).send_keys("Mar 09, 2022 - Mar 13, 2022")
    
  • Using XPATH:

    driver.execute_script("arguments[0].removeAttribute('readonly')", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[@class='form-control travel-date-section' and @name='daterangepicker']"))))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='form-control travel-date-section' and @name='daterangepicker']"))).send_keys("Mar 09, 2022 - Mar 13, 2022")
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

References

You can find a couple of relevant detailed discussins in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352