Python 表单填写自动化:为什么发送密钥会自动填写多个字段?

Python 表单填写自动化:为什么发送密钥会自动填写多个字段?,python,forms,selenium,automation,Python,Forms,Selenium,Automation,我在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="MainCont

我在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])
当我在Chromebrowser中运行文件时,endtime_字段被正确填充,但starttime_字段会自动填充endtime和starttime的条目,没有空格,即:

2020年4月27日上午8:30 2020年4月27日上午8:28

看起来第二个字段中的endtime_field.send_keys正在自动运行,尽管它有不同的XPath。我注意到从endtime开始的AM之后和从starttime开始的04之前没有空格

我尝试过使用Keys.TAB从endtime_字段移动到starttime_字段,并在填充之前执行starttime_字段.clear,但它们都不起作用

以下是使用浏览器检查时starttime_字段元素的外观:

<input name="ctl00$MainContent$startDt" type="text" id="MainContent_startDt" class="form-control">
任何帮助都将不胜感激。

发送键方法的工作原理与手动键入类似,并且它不考虑当前选择的焦点或上下文。 在您的例子中,在刷新/写入一个控件中的所有字符之前,焦点似乎要转移到另一个控件

从python selenium文档:

send_keys(*keys_to_send)
Sends keys to current focused element.
Args:   
keys_to_send: The keys to send. Modifier keys constants can be found in the ‘Keys’ class.

send_keys_to_element(element, *keys_to_send)
Sends keys to an element.
Args:   
element: The element to send keys.
keys_to_send: The keys to send. Modifier keys constants can be found in the ‘Keys’ class.
send_keys方法的工作原理与手动键入类似,它不考虑当前焦点或选择的上下文。 在您的例子中,在刷新/写入一个控件中的所有字符之前,焦点似乎要转移到另一个控件

从python selenium文档:

send_keys(*keys_to_send)
Sends keys to current focused element.
Args:   
keys_to_send: The keys to send. Modifier keys constants can be found in the ‘Keys’ class.

send_keys_to_element(element, *keys_to_send)
Sends keys to an element.
Args:   
element: The element to send keys.
keys_to_send: The keys to send. Modifier keys constants can be found in the ‘Keys’ class.

此代码顶部是否有任何循环或条件语句。当您选择相同的值时,您是否检查应用程序在没有自动化的情况下如何运行?此代码顶部是否有任何循环或条件语句。当您选择相同的值时,您是否检查应用程序在没有自动化的情况下如何运行?是的,似乎第一个发送键还没有停止过。您能否详细说明如何结束第一个控件?谢谢您可以使用starttime\u字段。在发送最后一行的\u键之前单击。很好,它工作正常-谢谢!仅供我学习-此单击完成“结束时间”字段。发送密钥[结束时间]”对吗?按照这个逻辑,在下一步之前,我需要在某个地方重新“单击”吗?是的,如果使用send_keys*keys_to_send方法,则需要通过单击移动当前焦点。是的,第一个send_键似乎还没有停止排序。您能否详细说明如何结束第一个控件?谢谢您可以使用starttime\u字段。在发送最后一行的\u键之前单击。很好,它工作正常-谢谢!仅供我学习-此单击完成“结束时间”字段。发送密钥[结束时间]”对吗?按照这个逻辑,在下一步之前,我需要在某个地方重新“单击”吗?是的,如果使用send_keys*keys_to_send方法,则需要使用click移动当前焦点。