从IE的下拉菜单中选择一个选项并触发功能 [英] Select an option from a dropdown in IE and triggering a function

查看:67
本文介绍了从IE的下拉菜单中选择一个选项并触发功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我是IE Automation for VBA的新手.我将尝试非常具体地回答我的问题.最近,我一直试图登录到一个网站,然后从下拉列表中选择一个月.我可以从下拉菜单中选择一个选项.但是,当我单击搜索按钮时,结果"不是针对使用VBA选择的值,而是针对网页上最初存在的值.通常,如果要访问该网站,则他必须单击下拉列表并选择相关时间段,然后单击搜索按钮.我也尝试了FireEvent"onchange",但这似乎没有任何效果.我将附加VBA的相关部分以及相关的HTML.

So, I am a newbiew in IE Automation for VBA.I will try to be very specific about my question. Recently, I have been trying to login to a website and then select a month from a dropdown list. I am able to select an option from the dropdown. However, when I click on the search Button, Results appear not for the values which I selected using VBA, but for the values that were originally there on the webpage. Normally, if one goes to the website, he has to click on the dropdown and select the relevant period and then click on the search button. I also tried FireEvent "onchange", but that does not seem to have any effect. I am attaching the relevant part of the VBA and also the related HTML.

请指导如何选择时间段.

Please guide how can I select the period.

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 ,则必须按照要求进行操作.

There can be two ways. The first is, you can set the needed parameters in the url. Than you don't need the step you want to do here. That solution works if the page uses Get to communicate with the server. If it uses Post, you must do your step you ask for.

您可以通过手动执行所有必需的步骤来检查它是否可以通过url参数起作用.之后,在浏览器中查找URL.如果有参数列表,则以问号(?)开头.以下所有参数均以&开头.

You can check if it works via url parameters by doing all needed steps manualy. After that, look in the url you find in the browser. If there is a list of parameters, it beginns with a questionmark (?). All following parameters beginn with an ampersand (&).

如果必须通过代码执行此操作,请尝试以下操作:

If you must do it via code try the following:

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事件的过程:

This procedure to trigger the html event:

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

这篇关于从IE的下拉菜单中选择一个选项并触发功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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