使用Excel VBA从网页日历弹出窗口中选择日期

时间:2018-10-25 11:39:35

标签: excel vba excel-vba

我需要从网页日历中选择日期,然后在其中应用过滤器。要设置的日期将在排除表单元格中。日期格式类似于“ 2018年9月10日”。 以下是我正在查看的网站的html DOM。请帮助:

<div class="datepicker__header"><a class="datepicker__navigation datepicker__navigation--previous">
</a><span class="datepicker__current-month">June 2018</span>
<a class="datepicker__navigation datepicker__navigation--next"></a>

<div class="weekday-container"><div class="datepicker__week-day">Mo</div><div class="datepicker__week-day">Tu</div><div class="datepicker__week-day">We</div>

<div class="datepicker__week-day">Th</div>

<div class="datepicker__week-day">Fr</div><div class="datepicker__week-day">Sa</div>
<div class="datepicker__week-day">Su</div></div></div>

<div class="datepicker__month"><div><div class="datepicker__day">28</div><div class="datepicker__day">29</div><div class="datepicker__day">30</div><div class="datepicker__day">31</div><div class="datepicker__day">1</div><div class="datepicker__day">2</div><div class="datepicker__day">3</div></div><div><div class="datepicker__day">4</div><div class="datepicker__day">5</div><div class="datepicker__day">6</div><div class="datepicker__day">7</div><div class="datepicker__day">8</div><div class="datepicker__day">9</div><div class="datepicker__day">10</div></div><div><div class="datepicker__day">11</div><div class="datepicker__day">12</div><div class="datepicker__day">13</div><div class="datepicker__day">14</div><div class="datepicker__day">15</div><div class="datepicker__day">16</div><div class="datepicker__day">17</div></div><div><div class="datepicker__day">18</div><div class="datepicker__day">19</div><div class="datepicker__day">20</div><div class="datepicker__day">21</div><div class="datepicker__day">22</div><div class="datepicker__day">23</div><div class="datepicker__day">24</div></div><div><div class="datepicker__day datepicker__day--selected">25</div><div class="datepicker__day">26</div><div class="datepicker__day">27</div><div class="datepicker__day">28</div><div class="datepicker__day">29</div><div class="datepicker__day">30</div><div class="datepicker__day">1</div></div></div>

1 个答案:

答案 0 :(得分:0)

我会这样尝试:如果弹出窗口还允许您键入以滚动弹出列表,那么它会起作用,但是如果它是真正的仅弹出窗口的选择,则可能不会。月份选择似乎是“类型”选择,而日期似乎是真正的弹出窗口(无论如何,我在这里看不到年份字段)。因此,尝试使用类似这样的方法,请使其适应您的特定代码需求

Set IE = CreateObject("InternetExplorer.Application")
Dim aNodeList, bNodeList As Object, cc,vv As Long

with IE
  Set aNodeList = .document.querySelectorAll("div[class=datepicker__month]")        
  For cc = 0 To aNodeList.Length - 1
      aNodeList.Item(cc).Value="Sep"
  Next cc

  Set bNodeList = .document.querySelectorAll("div[class=datepicker__day]")        
  For vv = 0 To bNodeList.Length - 1
        bNodeList.Item(vv).Value=10
  Next vv
End With

或者,作为替代方法(尝试最适合该特定页面的方法!):

Set IE = CreateObject("InternetExplorer.Application")
Dim aNodeList, bNodeList As Object, cc,vv As Long

With IE
  Set aNodeList = .document.getElementsByTagName("div")

  For Each element In aNodeList

      If element.class = "datepicker__day" Then
           element.Value=10
      End if

      If element.class = "datepicker__month" Then
           element.Value="Sep"
      End if
  Next

End With