python selenium提交按钮在日期选择器之后不工作

时间:2016-02-13 03:25:54

标签: javascript jquery python-2.7 date selenium-webdriver

我正在尝试使用selenium通过python提供日期字段。网站上的日期字段有一个日期选择器,其中显示了两个月。以下是该元素的详细信息。

<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">

我将光标放在该字段中,然后等待2秒直到出现日期选择器ID,并尝试通过选择日期的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()

我可以看到,当我尝试通过selenium调用firefox来模拟该操作时,在该字段中成功选择了日期。我的脚本中的下一行就是这个,

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

但我收到以下错误。

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>

请指出正确的方向。

注意:有时这是有效的,一次多次。 5次,1次正常工作。其余时间我收到此错误。

感谢。

2 个答案:

答案 0 :(得分:1)

要解决它需要做的几件事:

  • wait,提交按钮可点击
  • 滚动到元素的视图
  • 改进提交按钮XPath表达式

完整的工作代码:

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

答案 1 :(得分:1)

我认为这是你的问题,因为有几个输入有type = submit。它们实际上并不在网站上,而是在您点击的提交上方的dom中。

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

尝试并指定id“edit-submit”和名称“op”我不完全确定为什么Alecxe的答案不适合你。但我相当确定这就是你的问题所在。如果您在dom中执行control-f,则可以在顶部附近看到一个带有“input type ='submit'”的按钮。

尝试:

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