2

I'm trying to set some features on a page to download data automatically from a website. The problem is that I can't set the dates correctly from a calender.

I've tried setting the xpath, using the function 'find_element_by_xpath' but it doesn't seem to work.

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait


browser.get("http://www.aguacanal.es/regantes/")


select = Select(browser.find_element_by_id('sectorSelect'))
# select by visible text
select.select_by_visible_text('Sector III')

select = Select(browser.find_element_by_id('hidranteSelect'))
# select by visible text
select.select_by_visible_text('H03.156')


nav = browser.find_element_by_id('hidConDi-btn')
nav.click()



#---DatePicker
datefield = browser.find_element_by_id('hidConDi-fechaIni').click()
datefield.send_keys("01012019")
datefield = browser.find_element_by_id('hidConDi-fechaFin').click()
datefield.send_keys("03012019")

With this code I can select the current date, but I can't make it work correctly when I tried to set new dates.

1 Answer 1

1

You cannot simply send keys to the date picker and get it to select a date.

One way is to execute JS on the page to select the dates that you want, however this is considered a bad practice.

JS Injection Example:

#---DatePicker
browser.execute_script("$('#hidConDi-fechaIni').val('01/01/2019').change();")
time.sleep(2)
browser.execute_script("$('#hidConDi-fechaFin').val('03/03/2019').change();")

The better way would be to automate the navigation that you would normally do to select a date in the past, however it is more complex. Example

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.