所以,我是 VBA IE 自动化的新手。我会尝试非常具体地回答我的问题。最近,我一直在尝试登录一个网站,然后从下拉列表中选择一个月。我可以从下拉列表中选择一个选项。但是,当我单击搜索按钮时,显示的结果不是我使用 VBA 选择的值,而是网页上最初存在的值。通常,如果一个人访问该网站,他必须单击下拉菜单并选择相关时期,然后单击搜索按钮。
我还尝试了 FireEvent“onchange”,但这似乎没有任何效果。
我附上了 VBA 的相关部分以及相关的 HTML。
请指导我如何选择期间。
VBA代码:
Set e = IE.document.getElementsByName("fin")(0)
e.Focus
e.selectedIndex = 1
e.FireEvent ("onchange")
HTML:
<select name="fin" class="form-control ng-pristine ng-not-empty ng-valid ng-valid-required ng-touched"
data-ng-model="finyr" data-ng-options="item.year for item in years"
value="item.year" data-ng-change="hidedata()" required="">
<option label="2019-20" value="object:143" selected="selected">2019-20</option>
<option label="2018-19" value="object:144">2018-19</option>
<option label="2017-18" value="object:145">2017-18</option>
<option label="2020-21" value="object:146">2020-21</option></select>
可以有两种方法。首先,你可以在url中设置所需的参数。那么您不需要在这里执行您想要执行的步骤。如果页面使用该解决方案,则该解决方案有效Get
与服务器通信。如果它使用Post
,您必须执行您要求的步骤。
您可以手动执行所有需要的步骤,通过 url 参数检查它是否有效。之后,查看在浏览器中找到的网址。如果有参数列表,则以问号 (?) 开头。以下所有参数均以与号 (&) 开头。
如果您必须通过代码执行此操作,请尝试以下操作:
Sub SelectFromDropdown()
Dim url As String
Dim browser As Object
Dim nodeSelect As Object
url = "Your url here"
'Initialize Internet Explorer, set visibility,
'call URL and wait until page is fully loaded
Set browser = CreateObject("internetexplorer.application")
browser.Visible = True
browser.navigate url
Do Until browser.readyState = 4: DoEvents: Loop
'Get the dropdown and select the wanted entry by index
'Indexes beginn with 0
'(Code lines from your question)
Set nodeSelect = browser.document.getElementsByName("fin")(0)
nodeSelect.selectedIndex = 1
'It is likely that the change to the page must be communicated
'via an HTML event. This is probably the change event
'FireEvent doesn't work on most pages
Call TriggerEvent(browser.document, nodeSelect, "change")
End Sub
这个程序触发html事件:
Private Sub TriggerEvent(htmlDocument As Object, htmlElementWithEvent As Object, eventType As String)
Dim theEvent As Object
htmlElementWithEvent.Focus
Set theEvent = htmlDocument.createEvent("HTMLEvents")
theEvent.initEvent eventType, True, False
htmlElementWithEvent.dispatchEvent theEvent
End Sub
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)