Ad

Python Selenium Submit Button Not Working After Date Picker

I am trying to feed date field using selenium through python. Date field in the website has a date picker with two months shown in that. Here is the detail about that element.

<input id="depart-date-webform-client-form-1642116421-4" class="fcl-datepicker-widget-desktop fcl-datepicker-widget form-text required hasDatepicker" data-type="departing" data-cid="webform-client-form-1642116421-4" autocomplete="off" placeholder="dd/mm/yy" name="submitted[startDate]" value="" size="60" maxlength="128" type="text">

I am placing the cursor in that field, then wait for 2 seconds till the date picker id appears, and trying to feed the date by selecting the date's id.

self.driver.find_element_by_xpath("//input[@id='depart-date-webform-client-form-1642116421-4']").click()
    WebDriverWait(self.driver, 2).until(
            lambda d: d.find_elements_by_id('ui-datepicker-div')[0].is_displayed())
    self.driver.find_element_by_xpath("//div[@id='ui-datepicker-div']//td[@data-year='2016'][@data-month='2']/a[@class='ui-state-default'][text()='19']").click()

I can see that the date is selected successfully in the field when i try to simulate that action by calling firefox through selenium. Next line in my script is this,

self.driver.find_element_by_xpath('//input[@type="submit"]').click()

But I am getting the below error.

selenium.common.exceptions.WebDriverException: Message: Element is not clickable at point (136, 15.399993896484375). Other element would receive the click: <td class=" ui-datepicker-other-month ui-datepicker-unselectable ui-state-disabled"></td>

Please point me in the right direction.

Note : Sometime this is working, once in multiple times. Out of 5, 1 time it is working. Rest of the times I am getting this error.

Thanks.

Ad

Answer

Several things to be done to fix it:

  • wait for the submit button to be clickable
  • scroll into view of the element
  • improve the submit button XPath expression

The complete working code:

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


driver = webdriver.Firefox()
driver.maximize_window()
driver.get("http://www.flightcentre.co.nz/")

driver.find_element_by_xpath("//input[@id='depart-date-webform-client-form-1642116421-4']").click()

wait = WebDriverWait(driver, 2)
wait.until(lambda d: d.find_elements_by_id('ui-datepicker-div')[0].is_displayed())

driver.find_element_by_xpath("//div[@id='ui-datepicker-div']//td[@data-year='2016'][@data-month='2']/a[@class='ui-state-default'][text()='19']").click()

wait = WebDriverWait(driver, 10)
search = wait.until(EC.element_to_be_clickable((By.XPATH, '//div[@id = "edit-actions"]//input[@type="submit" and @name = "op"]')))

driver.execute_script("arguments[0].scrollIntoView()", search)
search.click()
Ad
source: stackoverflow.com
Ad