.GetElementsByName.SelectedIndex 明显更改选项但不是以编程方式?
.GetElementsByName.SelectedIndex visibly changes option but not programmatically?
我遇到了问题,不知道如何解决,在 Excel VBA 中,我有这段代码可以引导我浏览网站 (https://indexcalculator.ftserussell.com/),但是网站的第 3 步,当我通过 vba 更改选定的索引时,我可以看到它们在网页上发生了更改,但是当单击下一页按钮时,就好像它从未被单击过一样。
选定指数变化:
获取 Returns 后点击:
months = Format(DateSerial(year(Date), month(Date) - 1, 1), "m")
days = Format(DateSerial(year(Date), month(Date), 0), "d")
years = CInt(Format(DateSerial(year(Date), month(Date) - 1, 1), "yyyy")) - 1994
Application.Wait (Now + TimeValue("0:00:01"))
'If the year pops up blank that means that the base year is no longer 1994
HTMLdoc.getElementsByName("m_startDate")(0).selectedIndex = CInt(months - 1)
HTMLdoc.getElementsByName("d_startDate")(0).selectedIndex = CInt(days - 1)
HTMLdoc.getElementsByName("y_startDate")(0).selectedIndex = years
Application.Wait (Now + TimeValue("0:00:02"))
Set oButton = HTMLdoc.querySelector("a[href='javascript:submitForm(document.forms[0].action);']")
oButton.Click
Application.Wait (Now + TimeValue("0:00:01"))
Set oButton = HTMLdoc.querySelector("a[href='javascript:document.forms[0].target='_blank';submitForm('IndexDownload.aspx');']")
oButton.Click
第 4 步页面应该说的是 5 月 31 日而不是 6 月 13 日,当我手动执行时它有效。为什么会这样?
以下似乎工作正常。将 css select 或调整为您希望 select 结束日期的值。特别是看看我在下面大量使用的css attribute = value selector。
Option Explicit
Public Sub test()
Dim ie As Object, days As Long, months As Long, years As Long
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.Navigate2 "https://indexcalculator.ftserussell.com/"
While .Busy Or .readyState < 4: DoEvents: Wend
months = Format(DateSerial(Year(Date), Month(Date) - 1, 1), "m")
days = Format(DateSerial(Year(Date), Month(Date), 0), "d")
years = 1994
With .document
.querySelector("[value='irs3']").Click 'step 1
.querySelector("#Ctlnavigation2_lblControl [href*=action]").Click
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
.querySelector("#rdoSpDtRng").Click 'step 2
.querySelector("#Ctlnavigation2_lblControl [href*=action]").Click
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
.querySelector("[value='" & months & "']").Selected = True 'step 3
.querySelector("[value='" & days & "']").Selected = True
.querySelector("[value='" & years & "']").Selected = True
.querySelector("[name='m_endDate'] [value='" & months + 1 & "']").Selected = True
.querySelector("[name='d_endDate'] [value='" & days - 1 & "']").Selected = True
.querySelector("[name='y_endDate'] [value='" & years + 1 & "']").Selected = True
.querySelector("#Ctlnavigation2_lblControl [href*=action]").Click
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
End With
Stop '<delete me later
.Quit
End With
End Sub
我遇到了问题,不知道如何解决,在 Excel VBA 中,我有这段代码可以引导我浏览网站 (https://indexcalculator.ftserussell.com/),但是网站的第 3 步,当我通过 vba 更改选定的索引时,我可以看到它们在网页上发生了更改,但是当单击下一页按钮时,就好像它从未被单击过一样。
选定指数变化:
获取 Returns 后点击:
months = Format(DateSerial(year(Date), month(Date) - 1, 1), "m")
days = Format(DateSerial(year(Date), month(Date), 0), "d")
years = CInt(Format(DateSerial(year(Date), month(Date) - 1, 1), "yyyy")) - 1994
Application.Wait (Now + TimeValue("0:00:01"))
'If the year pops up blank that means that the base year is no longer 1994
HTMLdoc.getElementsByName("m_startDate")(0).selectedIndex = CInt(months - 1)
HTMLdoc.getElementsByName("d_startDate")(0).selectedIndex = CInt(days - 1)
HTMLdoc.getElementsByName("y_startDate")(0).selectedIndex = years
Application.Wait (Now + TimeValue("0:00:02"))
Set oButton = HTMLdoc.querySelector("a[href='javascript:submitForm(document.forms[0].action);']")
oButton.Click
Application.Wait (Now + TimeValue("0:00:01"))
Set oButton = HTMLdoc.querySelector("a[href='javascript:document.forms[0].target='_blank';submitForm('IndexDownload.aspx');']")
oButton.Click
第 4 步页面应该说的是 5 月 31 日而不是 6 月 13 日,当我手动执行时它有效。为什么会这样?
以下似乎工作正常。将 css select 或调整为您希望 select 结束日期的值。特别是看看我在下面大量使用的css attribute = value selector。
Option Explicit
Public Sub test()
Dim ie As Object, days As Long, months As Long, years As Long
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.Navigate2 "https://indexcalculator.ftserussell.com/"
While .Busy Or .readyState < 4: DoEvents: Wend
months = Format(DateSerial(Year(Date), Month(Date) - 1, 1), "m")
days = Format(DateSerial(Year(Date), Month(Date), 0), "d")
years = 1994
With .document
.querySelector("[value='irs3']").Click 'step 1
.querySelector("#Ctlnavigation2_lblControl [href*=action]").Click
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
.querySelector("#rdoSpDtRng").Click 'step 2
.querySelector("#Ctlnavigation2_lblControl [href*=action]").Click
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
.querySelector("[value='" & months & "']").Selected = True 'step 3
.querySelector("[value='" & days & "']").Selected = True
.querySelector("[value='" & years & "']").Selected = True
.querySelector("[name='m_endDate'] [value='" & months + 1 & "']").Selected = True
.querySelector("[name='d_endDate'] [value='" & days - 1 & "']").Selected = True
.querySelector("[name='y_endDate'] [value='" & years + 1 & "']").Selected = True
.querySelector("#Ctlnavigation2_lblControl [href*=action]").Click
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
End With
Stop '<delete me later
.Quit
End With
End Sub