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.
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.
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)
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.
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.
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.
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