Select an option from a dropdown in IE and triggering a function

半城伤御伤魂 提交于 2021-02-16 21:22:17

问题


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 code:

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>

回答1:


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.

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

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


来源:https://stackoverflow.com/questions/61680941/select-an-option-from-a-dropdown-in-ie-and-triggering-a-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!