0

I want to select a date from this DatePicker :

So far, I have been able to select today's date only. But I don't know how to select a specific date. From this I need to select yesterday's date. Here what I've done so far:

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(chromedriver)
driver.get('https://www.ut.com.sv/reportes?p_auth=vUIS4sqn&p_p_id=MenuReportesEstadisticos_WAR_CompletePublicReports&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_MenuReportesEstadisticos_WAR_CompletePublicReports_reportName=25cargosdelsistema')

elm = driver.find_element_by_link_text('Cargos del Sistema')
driver.implicitly_wait(5)
elm.click()

#TIPO
drpmedicion=driver.find_element_by_id('tipo')
drpmed=Select(drpmedicion)
driver.implicitly_wait(2)

drpmed.select_by_visible_text('Indicativo')
driver.implicitly_wait(2)

#SALIDA
drpsalida=driver.find_element_by_id('outType')
drpsal=Select(drpsalida)
driver.implicitly_wait(2)
drpsal.select_by_visible_text('csv')

#FECHAS INICIO
date1=driver.find_element(By.NAME,'fecha_desde')
date1.click()
driver.implicitly_wait(2)
date1.click()
driver.implicitly_wait(2)

date2=driver.find_element(By.CLASS_NAME,'picker__button--today')
date2.click()

driver.implicitly_wait(5)

#FECHAS FIN
date3=driver.find_element(By.NAME,'fecha_hasta')
driver.implicitly_wait(2)
date3.click()
driver.implicitly_wait(2)
date3.click()

driver.implicitly_wait(5)

date4=driver.find_elements(By.CLASS_NAME,'picker__button--today')
date4[1].click()

driver.implicitly_wait(5)


#CLICK FINAL
btn=driver.find_element(By.XPATH, '//*[@id="portlet_FormularioReportesEstadisticos_WAR_CompletePublicReports"]/div/div/div/input')
btn.click()
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the [CC BY-SA 4.0 license](//creativecommons.org/licenses/by-sa/4.0/), for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted. If you want to know more about deleting a post please see: [How does deleting work?](//meta.stackexchange.com/q/5221) – Dharman Jun 26 '20 at 18:42

1 Answers1

0

You can approach this a couple of ways, the easiest would be to get the element based on the attribute data-pick that the linked website uses:

<td role="presentation">
    <div class="picker__day picker__day--infocus" data-pick="1590966000000" role="gridcell">1</div>
</td>

The value is the day's epoch equivalent in milliseconds which you can get like so (change it to whatever date format you use)

import time
import datetime

selectedDate = "01/06/2020"
epoch = int(time.mktime(datetime.datetime.strptime(selectedDate, "%d/%m/%Y").timetuple()))*1000

Add the epoch value into your selector, get the parent element from there and click it.

The other way to click it would be to execute 'raw' Javascript via Selenium's execute_script. include/js/pickadate/picker.js handles when you click on the date with an on('click'... event. You could invoke that by using a querySelector and then using the .click() method.

Lucan
  • 2,907
  • 2
  • 16
  • 30
  • Could you expand a bit more on how to get the parent element? So far I have done a few things like: date=driver.find_element_by_css_selector('div[data-pick="1591164000000"') but now I can get the parent element to click it. – David Diaz Jun 03 '20 at 05:26
  • [This answer](https://stackoverflow.com/questions/18079765/how-to-find-parent-elements-by-python-webdriver) is probably the easiest way to get the parent. So from `date` you would get the xpath of `..` which should be one up from that element, thus the parent. – Lucan Jun 03 '20 at 08:44