Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select from a DatePicker in Python

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.

like image 662
Mudit Gupta Avatar asked Apr 10 '18 06:04

Mudit Gupta


People also ask

How does Selenium Python select date from Datepicker?

We can select a date from a datepicker with Selenium webdriver using Python. To identify a particular date, first we have to use the find_elements method and identify all the dates having a common locator value. The find_elements returns a list of matching elements.

How does Selenium interact with calendar?

Step 1 - Initiate WebDriver and set the application's URL where a Calendar is implemented. Define the xpath with the Calendar control's id (in this example it is - travel_date) from the html source to find the element and later use that object to initiate the click action.

How does Selenium handle date pickers?

Find the locator for the Month (use Firebug/Firepath) This is probably a Select element, use Selenium to select Month. Do the same for Year. Click by linkText "31" or whatever date you want to click.


2 Answers

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:

  • Calendar date picker Selenium Python
like image 172
undetected Selenium Avatar answered Oct 04 '22 10:10

undetected Selenium


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()
like image 44
Frank Avatar answered Oct 04 '22 10:10

Frank