如何使用Selenium Python单击活动日期选择器? [英] How do I click on active day on date picker using selenium python?

查看:65
本文介绍了如何使用Selenium Python单击活动日期选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此页面上有多个日历(日期选择器),我设法单击了所需的日历.

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

我想单击有效日期(当前日期).class =活动日"单击所需的日历字形后,如何找到该元素?

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.当我为此复制选择器时-我得到了

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

当我检查元素并搜索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()

但是却收到 ElementNotVisibleException

我希望使用通用脚本来执行此操作.我想在此日历上选择有效日期.单击xpath无效,因为它没有绝对路径.

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.

推荐答案

单个HTML页面可能有多个日期选择器如果我们要单击特定日期选择器的有效日期,则可以使用选择器

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

其中"9"表示是该日期选择器的出现.即页面上有9个或更多datepicker元素.

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

因此,对于上面的问题,孩子是27岁,因此下面的代码将单击日期选择器的有效日期(当前日期).

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.即使您设法单击日历xpath,单击活动日期也将需要日历的第n个子级值.

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

这篇关于如何使用Selenium Python单击活动日期选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆