Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用selenium自动填写日期_Python_Html_Selenium - Fatal编程技术网

Python 使用selenium自动填写日期

Python 使用selenium自动填写日期,python,html,selenium,Python,Html,Selenium,我正在尝试使用selenium在以下网站上填写表格。 然而,我正在努力设置日期(甚至让日期选择器出现) 通过使用以下代码,我可以看到值存储在那里: from selenium import webdriver from selenium.webdriver.firefox.options import Options browser = webdriver.Firefox() browser.get('https://bookings.doc.govt.nz/Saturn/Facilities

我正在尝试使用selenium在以下网站上填写表格。

然而,我正在努力设置日期(甚至让日期选择器出现)

通过使用以下代码,我可以看到值存储在那里:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
browser = webdriver.Firefox()
browser.get('https://bookings.doc.govt.nz/Saturn/Facilities/SearchViewGW.aspx')

el = browser.find_element_by_id('mainContent_homeContent_txtArrivalDate')
el.get_property('value')
el.clear()
el.send_keys("21122018")
但是,清除日期并发送新密钥会导致错误弹出。
关于需要单击哪个元素来显示日期选择器,或者如何直接设置所需日期的任何建议?

您引用的字段不允许您单击它并键入数字,因此您需要采取另一种方法

请在此处查看此指南:

提供的代码是Java,但Selenium API应该与您需要采用的方法类似

总之,您需要编写如下代码(摘自文章):

//要在文本框中设置的日期和时间
字符串dateTime=“2014年7月12日下午2:00”;
WebDriver=newfirefoxdriver();
driver.manage().window().maximize();
驱动程序。获取(“http://demos.telerik.com/kendo-ui/datetimepicker/index");
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
//打开日历的按钮
WebElement selectDate=driver.findElement(By.xpath(//span[@aria controls='datetimepicker\u dateview']);
选择日期。单击();
//在日历中移动下一步的按钮
WebElement nextLink=driver.findElement(By.xpath(“//div[@id='datetimepicker\u dateview']//div[@class='k-header']///a[contains(@class,'k-nav-next')]);
//单击日历标题中心的按钮
WebElement midLink=driver.findElement(By.xpath(“//div[@id='datetimepicker\u dateview']//div[@class='k-header']]//a[contains(@class,'k-nav-fast')]);
//在日历中移动上个月的按钮
WebElement previousLink=driver.findElement(By.xpath(“//div[@id='datetimepicker\u dateview']//div[@class='k-header']///a[contains(@class,'k-nav-prev')]);
//拆分日期时间以仅获取日期部分
字符串date\u dd\u MM\u yyyyy[]=(dateTime.split(“”[0]).split(“”/“”);
//获取当前年份与要在日历中设置的年份之间的年份差
int yearDiff=Integer.parseInt(date_dd_MM_yyyy[2])-Calendar.getInstance().get(Calendar.YEAR);
midLink.click();
如果(年差!=0){
//如果你明年要搬家的话
如果(年差>0){
对于(int i=0;i”+i);
单击();
}
}
//如果你上一年不得不搬家

否则,如果(yearDiff尝试此操作,只需使用JavaScript设置元素的value属性。 这可能不是您正在寻找的方法,如果需要,请用更好的方法替换time.sleep(等待元素可用)

我已将日期硬编码为2018年10月20日

from selenium.webdriver import Chrome

driver = Chrome()
driver.get('https://bookings.doc.govt.nz/Saturn/Facilities/SearchViewGW.aspx')

import time
time.sleep(5)

driver.execute_script(
   "document.getElementById(
       'mainContent_homeContent_txtArrivalDate').setAttribute('value', '20/10/2018')"
)
from selenium.webdriver import Chrome

driver = Chrome()
driver.get('https://bookings.doc.govt.nz/Saturn/Facilities/SearchViewGW.aspx')

import time
time.sleep(5)

driver.execute_script(
   "document.getElementById(
       'mainContent_homeContent_txtArrivalDate').setAttribute('value', '20/10/2018')"
)