1

I am trying to input date on a web page using selenium. I've used the code as follows:

from selenium import webdriver

driver = webdriver.Firefox()
driver.get('https://www.nccpl.com.pk/en/market-information/fipi-lipi/lipi-sector-wise-daily') 

datefield = driver.find_element_by_id('popupDatepicker')
datefield = driver.find_element_by_name('value')
datefield.click()
datefield.send_keys("01/01/2019")

I am unable to plug in the desired date "01/01/2019". I searched for similar solutions and found this link.

As suggested in the above mentioned solution I tried using ActionChains to perform one task after another but still I was unable to change the date. The piece of code used to perform ActionChains is as follows:

ActionChains(driver).move_to_element(datefield).click().send_keys('01/01/2019').perform()

Any idea if I am doing something wrong or how can I get it working?

Furqan Hashim
  • 1,304
  • 2
  • 15
  • 35

1 Answers1

1

Input field #popupDatepicker has attribute readonly. That means that you cannot send keys to it.

Try below to select required date

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium import webdriver


driver = webdriver.Firefox()
driver.get('https://www.nccpl.com.pk/en/market-information/fipi-lipi/lipi-sector-wise-daily') 
picker = wait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'popupDatepicker')))
driver.execute_script('arguments[0].scrollIntoView();', picker)
picker.click()
wait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[title="Select Tuesday, Jan 1, 2019"]'))).click()

If you need to select month or year you need to handle select drop-down:

from selenium.webdriver.support.ui import Select 

select_month = Select(wait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[title="Change the month"]'))))
select_month.select_by_visible_text('April')
select_year = Select(wait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[title="Change the year"]'))))
select_year.select_by_visible_text('2018')
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • I am getting this error now `selenium.common.exceptions.TimeoutException: Message:` even though there is wait time. – Furqan Hashim Jan 20 '19 at 17:41
  • @FurqanHashim , on which line? – Andersson Jan 20 '19 at 17:49
  • @FurqanHashim , no I mean show me that line. I don't know what is 17th line – Andersson Jan 20 '19 at 17:53
  • It is this line: `wait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'popupDatepicker'))).click()`. Moreover, I think since the page is not scrolled down therefore this line fails clicking the date pop up. If I manually scroll when the page pops up the code works perfectly fine. – Furqan Hashim Jan 20 '19 at 17:55
  • Still same error at this line `picker = wait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'popupDatepicker')))` – Furqan Hashim Jan 20 '19 at 18:06
  • @FurqanHashim , Hm... Try to replace `element_to_be_clickable` with `presence_of_element_located` – Andersson Jan 20 '19 at 18:10