简体   繁体   中英

How can I fill web forms that use a drop down calendar for picking dates with Python?

Right now I'm trying to fill this form.

I'm using selenium to select this input box with a drop down calendar. When I inspect the input box, I get:

*日期表格框的html *

When I try to select it by one of these methods:

form_element = browser.find_element_by_name('date')
form_element = browser.find_element_by_xpath("//input[@name='date']")
form_element = browser.find_element_by_xpath("//input[@type='date']")

I get this error with one of the following "Unable to locate element:" messages:

selenium.common.exceptions.NoSuchElementException: Message: no such element: 
Unable to locate element: {"method":"name","selector":"date"}
Unable to locate element: "method":"xpath","selector":"//input[@name='date']"}
{"method":"name","selector":"date"}

What am I doing wrong? Is there something else I'm supposed to select or is there another way to do this? Is it even possible with selenium? if not, how else can I fill such forms?

The basic idea to interact with form is to select the form element (by id, by name, by class), activate the editing mode (by simulating a click or a return key), write inside it and after move to the next element.

Documentation: http://selenium-python.readthedocs.io/locating-elements.html

For editing the date field in particular, you select it, you activate the editing and you compile the date components one by one (day, month, year).

date= driver.find_element_by_name("date")
date.send_keys(Keys.RETURN)
date.send_keys("10")
date.send_keys("10")
date.send_keys("2016")

For your specific web page here it is all the working code.

driver = webdriver.Chrome()
driver.get("http://frontop.ca/rtm/eglintongis/dataenterpage.html?pj=MD")

date= driver.find_element_by_name("date")
date.send_keys(Keys.RETURN)
date.send_keys("10")
date.send_keys("10")
date.send_keys("2016")

time= driver.find_element_by_name("time")
time.send_keys(Keys.RETURN)
time.send_keys("15:00")
time.send_keys(Keys.RETURN)

temperature= driver.find_element_by_name("temp")
temperature.send_keys(Keys.RETURN)
temperature.send_keys("5")

operator= driver.find_element_by_name("oper")
operator.send_keys("BlaBLABLA")

select= driver.find_element_by_name("itemslt")
select.click()

date= driver.find_element_by_name("reading")
date.send_keys(Keys.RETURN)
date.send_keys("BlaBLABLA")

submit= driver.find_element_by_name("sltdate")
submit.click()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM