How do I click on active day on date picker using selenium python?

醉酒当歌 提交于 2021-01-29 10:54:54

问题


So there are multiple calendars (datepickers) on the page and I have managed to click on the desired calendar.

I want to click on the active date (current date). class="day active" How can I find this element after clicking on desired calendar glyphicon?

P.S. when I copy selector for this- I am getting

body > div:nth-child(27) > div.datepicker-days > table > tbody > tr:nth-child(4) > td.day.active

And when I inspect element and search td.day.active, I am able to reach that element.

So I have used below syntax for clicking on it.

WebDriverWait(driver, 30).until(lambda driver: driver.find_element_by_css_selector('td.day.active')).click()

But getting ElementNotVisibleException

I want a generic script to do this. I want to select the active date on this calendar. clicking on xpath doesnt work as there is no absolute path to it.


回答1:


There can be a possibility where a single html page has more than one datepickers If we want to click on active date of specific datepicker then selector can be used

.dropdown-menu:nth-child(9) > .datepicker-days .active

where "9" is the occurence of that datepicker. i.e. there are 9 or more datepicker elements on the page.

So for above question, child is 27 hence below code will click on active date (current date) of datepicker.

WebDriverWait(driver, 30).until(lambda driver: driver.find_element_by_css_selector(".dropdown-menu:nth-child(27) > .datepicker-days .active")).click()

P.S. Even if you manage to click on calendar xpath, clicking on active date will require nth-child value of calendar.



来源:https://stackoverflow.com/questions/64516795/how-do-i-click-on-active-day-on-date-picker-using-selenium-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!