How can I fill out web forms that use a dropdown calendar to select dates using Python?

I am currently trying to fill out this form.

I am using selenium to select this dropdown calendar input box. When I check the input box, I get:

* The html of the date form box *

When I try to select it in one of the following ways:

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 am getting this error with one of the following "Cannot find item:" 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 that I should choose or is there another way to do this? Is this possible with selenium? if not, how else can i fill out such forms?

+3


source to share


1 answer


The main idea of ​​interacting with a form is to select a form element (by id, by name, by class), activate the edit mode (by simulating a click or the return key), write inside it, and after moving to the next element.

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

To edit a date field, specifically, you select it, you activate the editing and compose 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's 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()

      

+1


source







All Articles