【发布时间】:2020-04-27 10:41:32
【问题描述】:
我在 Windows 中使用 Python 和 Selenium 编写以下内容,目的是在两个字段中填写当前日期和每个字段的指定时间:
#Defines and inputs date and time into End Time field
x = datetime.datetime.now()
endtime = x.strftime("%m/%d/%Y" " 8:30 AM")
endtime_field = driver.find_element_by_xpath('//*[@id="MainContent_endDt"]')
endtime_field.send_keys([endtime])
#Defines and inputs date and time into Start Time field
starttime = x.strftime("%m/%d/%Y" " 8:28 AM")
starttime_field = driver.find_element_by_xpath('//*[@id="MainContent_startDt"]')
starttime_field.send_keys([starttime])
当我在Chrome浏览器中运行该文件时,endtime_field 填写正确,但starttime_field 自动填写了结束时间和开始时间的条目,没有空格,即:
04/27/2020 8:30 AM04/27/2020 8:28 AM
似乎 endtime_field.send_keys 在第二个字段中自动运行,即使它具有不同的 XPath。我注意到endtime 的 AM 之后和starttime 的 04 之前没有空格。
我尝试使用Keys.TAB 从endtime_field 移动到starttime_field,并在填写之前执行starttime_field.clear(),但它们都不起作用。
这是使用浏览器检查时starttime_field 元素的样子:
<input name="ctl00$MainContent$startDt" type="text" id="MainContent_startDt" class="form-control">
任何帮助将不胜感激。
【问题讨论】:
-
这段代码上面是否有任何循环或条件语句。当您选择相同的值时,您是否检查了应用程序在没有自动化的情况下的行为?
标签: python forms selenium automation