Get and automate ASP Datepicker control using Python and Selenium

This article uses Python 3.6.0 and Selenium 3.4.0 (WebDriver is Google Chrome).

I had a hard time getting the Datepicker control (dropdown calendar) created by ASP, so I will leave a note.

Datepicker control is Table

The Datepicker control is a control that drops down the calendar when clicked. When I examined the dropped control in Google Chrome, I found that it consisted of a Table tag. Therefore, it can be obtained in the following order.

  1. Click the Datepicker control
  2. Get the generated calendar Table tag
  3. Do what you want to do

Sample to get the date automatically by specifying the date

I made a function that clicks the corresponding date when the desired date is specified.

DatepickerMove.py



#Pass the variable from which the page with the Datepicker control is retrieved to the browser
#Make sure the Datepicker control is clicked before executing the function

def DatepickerMove(browser, year, month, day):
    #Click the title to display the year / month selection screen
    calendar_title = browser.find_element_by_id('CalendarExtender1_title')
    calendar_title.click()

    calendar_prev = browser.find_element_by_id('CalendarExtender1_prevArrow')
    calendar_next = browser.find_element_by_id('CalendarExtender1_nextArrow')

    time.sleep(1)
    #Check the year. If it is different, move it in the corresponding direction depending on the size
    #Limit the loop in case of runaway
    limit_count = 100
    for i in range(limit_count):
        if calendar_title.text == year:
            break
        elif calendar_title.text > year:
            calendar_prev.click()
        elif calendar_title.text < year:
            calendar_next.click()
    else:
        print('The specified number of processes has been exceeded')
        return

    time.sleep(1)

    #Get monthly
    month_title = year + 'Year' + month + 'Month'
    calendar_month = browser.find_element_by_xpath('//div[@title=\"' + month_title + '\"]')
    calendar_month.click()

    time.sleep(1)

    #Get the date
    day_title = month_title + day + 'Day'
    calendar_day = browser.find_element_by_xpath('//div[@title=\"' + day_title + '\"]')
    calendar_day.click()

    time.sleep(1)

Why you are clicking on the title

DatepickerMove.py



browser.find_element_by_id('CalendarExtender1_title')
calendar_title.click()

calendar_prev = browser.find_element_by_id('CalendarExtender1_prevArrow')
calendar_next = browser.find_element_by_id('CalendarExtender1_nextArrow')

Clicking on the Datepicker control will generate a calendar for the date (ie a regular calendar). If you click on the title (for example, the part that says "April, 2017"), This time, the year and January-December can be selected instead of the date. The arrangement from January to December is different from the date, because it has 12 buttons and the arrangement is fixed and easy to handle. It has changed to the state of the year and month once.

We also have an element that moves to the previous year and the next year when clicked.

Why click the previous year and the next year to select the year

DatepickerMove.py



    limit_count = 100
    for i in range(limit_count):
        if calendar_title.text == year:
            break
        elif calendar_title.text > year:
            calendar_prev.click()
        elif calendar_title.text < year:
            calendar_next.click()
    else:
        print('The specified number of processes has been exceeded')
        return

If you click the title in the same way as above, the year will change to selectable. However, since the arrangement of years changes depending on the current year, I thought that it would be safer to loop and check every year, so I did not think so much.

Why XPath is getting from title

DatepickerMove.py



    #Get monthly
    month_title = year + 'Year' + month + 'Month'
    calendar_month = browser.find_element_by_xpath('//div[@title=\"' + month_title + '\"]')
    calendar_month.click()

    time.sleep(1)

    #Get the date
    day_title = month_title + day + 'Day'
    calendar_day = browser.find_element_by_xpath('//div[@title=\"' + day_title + '\"]')
    calendar_day.click()

In the case of a date, the date of the previous month may be selected depending on the calendar. For example, if you try to get March 25, 2017 and specify 25 in the text, you will get February 25, 2017. Therefore, I thought that it would be safer to select the full name in the title, so I did this.

in conclusion

It's been less than a month since I first learned Python, so coding may be a problem. However, it's a fun language that you can easily do various things even with shallow experience points.

Recommended Posts

Get and automate ASP Datepicker control using Python and Selenium
Get and set the value of the dropdown menu using Python and Selenium
I tried web scraping using python and selenium
Python programming: I tried to get (crawling) news articles using Selenium and BeautifulSoup4.
Automate Chrome with Python and Selenium on your Chromebook
Get files from Linux using paramiko and scp [Python]
Start to Selenium using python
Instrument control using Python [pyvisa]
[python] Get quotient and remainder
Challenge Python3 and Selenium Webdriver
Serial communication control with python and I2C communication (using USBGPIO8 device)
Get an English translation using python google translate selenium (memories)
Serial communication control with python and SPI communication (using USBGPIO8 device)
Authentication using tweepy-User authentication and application authentication (Python)
Scraping with Python, Selenium and Chromedriver
How to get followers and followers from python using the Mastodon API
Clustering and visualization using Python and CytoScape
Selenium and python to open google
Get and estimate the shape of the head using Dlib and OpenCV with python
Notes using cChardet and python3-chardet in Python 3.3.1.
Python> dictionary> values ()> Get All Values by Using values ()
Get Suica balance in Python (using libpafe)
Get tweets containing keywords using Python Tweepy
Using Python and MeCab with Azure Databricks
[Control engineering] Graphing transfer functions using Python
Get Twitter bookmarks on CentOS using Selenium
Get Youtube data in Python using Youtube Data API
Reboot the router using Python, Selenium, PhantomJS
Practice web scraping with Python and Selenium
I'm using tox and Python 3.3 with Travis-CI
Head orientation estimation using Python and OpenCV + dlib
Notes on installing Python3 and using pip on Windows7
Develop and deploy Python APIs using Kubernetes and Docker
[Python] Get all comments using Youtube Data API
Try running Google Chrome with Python and Selenium
Python development flow using Poetry, Git and Docker
I tried object detection using Python and OpenCV
Get image URL using Flickr API in Python
Get git branch name and tag name with python
Get note information using Evernote SDK for Python 3
Create a web map using Python and GDAL
Drag and drop local files with Selenium (Python)
Let's control EV3 motors and sensors with Python
[Python3] Automatic sentence generation using janome and markovify
Try using tensorflow ① Build python environment and introduce tensorflow
Get Gmail subject and body with Python and Gmail API
Create a Mac app using py2app and Python3! !!
Try using ChatWork API and Qiita API in Python
I want to get custom data attributes of html as elements using Python Selenium
I tried to automate the article update of Livedoor blog with Python and selenium.