我试图在下个月从使用selenium和beautifulsoup的日期选择器中选择同一天但是beautifulsoup总是获取开始月份的日期元素。 datepicker将所有日期保存为<td>
,因此我使用find_all('td')
并运行for循环来检查下个月的当前日期是否已经可见。如果没有,我用selenium点击下个月的按钮,找到另一个循环的元素。
# activating datepicker. need to select the date now
driver.find_element_by_name("datepicker").click()
bsObj = BeautifulSoup(pageSource, 'lxml')
calendar = bsObj.find_all('td')
today_index = -1
searching = True
# check if there are 30 days on the visible calendar after the current day
for i in range(len(calendar)):
calendar[i] = calendar[i].div
print calendar[i].text
if day == calendar[i].text and searching:
today_index = i
searching = False
print calendar
# if there are 30 days left, select the day on the calendar
if (len(calendar) - today_index) >= 30:
thirty_from_today = calendar[today_index + 30]
# if there are not 30 days left, move to next month and find 30th day from today
else:
searching = True
driver.find_element_by_css_selector("*[title='Next month']").click()
next_month_calendar = bsObj.find_all('td')
print 'MOVING TO NEXT MONTH', next_month_calendar
for i in range(len(next_month_calendar)):
next_month_calendar[i] = next_month_calendar[i].div
print next_month_calendar[i].text
if day == next_month_calendar[i].text and searching:
searching = False
print 'setting thirty_from_today variablr too', next_month_calendar[i]
thirty_from_today = next_month_calendar[i]
我可以看到next_month_calendar变量仍然显示3月的所有日期(以及之前和之后的一些日期,因为日期选择器一次显示的时间超过一个月),即使在我激活下个月按钮以显示日期之后也是如此在四月,但我不明白为什么因为我可以看到日期选择器更改为四月,因为selenium控制我的浏览器。我可以说日期肯定是错误的,因为每个<td>
元素在Unix时间都有一个日期数据元素。