5

Can anyone help me with the Selenium webdriver (in Python) code to automatically select a date in the input date in the above link.

https://www.nseindia.com/products/content/derivatives/equities/archieve_fo.htm

It is just one line of code but I've wasted hours behind it. someone please kindly help. I've tried the .send_keys() function and have searched for hours about the datepicker issue. Kindly help.

Frank
  • 831
  • 1
  • 11
  • 23
Mudit Gupta
  • 53
  • 1
  • 1
  • 3
  • Go through this link might help you. https://stackoverflow.com/questions/21422548/how-to-select-the-date-picker-in-selenium-webdriver – Alok Apr 10 '18 at 07:12
  • See my answer below. Why do you think that it needed just one line of code? – Frank Apr 10 '18 at 07:24

3 Answers3

7

You havn't mentioned exactly where you are stuck while automatically sending a date in the <input> tag. How ever the <input> tag is having type as text and the following code block works perfect :

  • Code Block :

     from selenium import webdriver
    
     driver=webdriver.Firefox(executable_path=r'C:\path\to\geckodriver.exe')
     driver.get("https://www.nseindia.com/products/content/derivatives/equities/archieve_fo.htm")
     print("Page Title is : %s" %driver.title)
     driver.find_element_by_xpath("//input[@class='textboxdata hasDatepicker' and @id='date']").send_keys("10-04-2018")
    
  • Console Output :

     Page Title is : NSE - National Stock Exchange of India Ltd.
    
  • Snapshot :

NSE


References

You can find a couple of relevant detailed discussion in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I agree that this works fine but are you testing the datepicker in this way? – Frank Apr 10 '18 at 07:54
  • @Frank OP's question heading reads as _Select a date in a website_ and in the question body OP mentioned about using `send_keys()` but not `@Test` _Datepicker_ functionality. Thanks – undetected Selenium Apr 10 '18 at 08:17
  • but he "searched for hours about the datepicker" so that's why my answer focusses on the datepicker. – Frank Apr 10 '18 at 09:14
  • @Frank While handling _Datepicker_ functionality the procedure you followed is the best way. But OP explicitly mentioned about `send_keys()` hence my Answer. – undetected Selenium Apr 10 '18 at 09:18
  • Thank you so much, Sir. But there seems to be a problem. When I run this code (opening chrome), it did print the title but then it gave me this error. Traceback (most recent call last): selenium.common.exceptions.WebDriverException: Message: unknown error: call function result missing 'value' – Mudit Gupta Apr 11 '18 at 11:14
  • `selenium.common.exceptions.WebDriverException: Message: unknown error: call function result missing 'value'` is a common error due to mismatch in binary versions and you can solve it following the discussion [unknown error: call function result missing 'value' for @MuditGupta Selenium Send Keys even after chromedriver upgrade](https://stackoverflow.com/questions/49162667/unknown-error-call-function-result-missing-value-for-selenium-send-keys-even/49452443#49452443) – undetected Selenium Apr 11 '18 at 11:16
  • The calendar popup is not dismissed after send keys. How can I make the calendar dismissed? – Heinz Dec 30 '19 at 20:13
4

For instance clicking on the 5 of march 2017:

driver.get("https://www.nseindia.com/products/content/derivatives/equities/archieve_fo.htm")

datepicker = driver.find_element_by_id("date")
datepicker.click()

selectMonth = driver.find_element_by_xpath('//select[@class="ui-datepicker-month"]')
for option in selectMonth.find_elements_by_tag_name('option'):
    if option.text == 'Mar':
        option.click() 
        break

selectYear = driver.find_element_by_xpath('//select[@class="ui-datepicker-year"]')
for option in selectYear.find_elements_by_tag_name('option'):
    if option.text == '2017':
        option.click() 
        break 

days = driver.find_elements_by_xpath('//a[@class="ui-state-default"]')
days[4].click()
Frank
  • 831
  • 1
  • 11
  • 23
2

If Jquery is included:

driver.execute_script('$("#date_element").val("1977-01-22")')

else:

driver.execute_script('document.getElementById('date_element').value ="1977-01-22"')