PDA

View Full Version : [SOLVED] IE automation: able to change dropdown list value but webpage doesn't update



Sandstedt
11-15-2012, 10:53 AM
The webpage: https://www.avanzabank.se/aktier/lista

I want it to update when I change the value of the second dropdown list "Lista".

The list:

<select id="list" class="list" name="listKey">
<option value="large_cap_se"></option>
<option value="mid_cap_se"></option>
<option value="small_cap_se"></option>
<option value="firstnorth_se"></option>
<option value="aktietorget"></option>
<option value="ngm_equity"></option>
<option value="ngm_mtf"></option>
<option value="inofficiella"></option>
<option value="xterna_listan"></option>
</select>
VBA-code:
IE(0).Document.getElementById("list").SelectedIndex = 2
IE(0).Document.getElementById("list").FireEvent ("onchange")
I have also tried:
IE(0).Document.getElementById("list").onchange
without success.

Sandstedt
11-16-2012, 01:16 AM
From what I understand, the dropdown list should trigger a javascript function but is unable to do so when I change its value in VBA.

I have tried to find the name of the function and its parameters, but my knowledge of html and javascript is very limited. Is it possible to trigger the script by some kind of change-, click-, or mouseup-event?

Edit: It might be this function:


<script type="text/javascript">
var steal = { startFile: "avanzabank", env: "production", loaded: ['avanzabank/production.css'], production: "/js/jmvc/avanzabank/production.js?ver=r90645.10" }
</script>

Crocus Crow
11-16-2012, 04:55 AM
IE(0).Document.getElementById("list").SelectedIndex = 2
That should work, but why are you using an array, IE(0)? Only a single IE object should be needed -

IE.Document.getElementById("list").SelectedIndex = 2
- unless you are using an array of InternetExplorer objects. Post your code in full if you want more help.


From what I understand, the dropdown list should trigger a javascript function but is unable to do so when I change its value in VBA.No, looking at the HTML source, there is no onclick or other event handler in the <select id="list" ...> element, so clicking it manually or by VBA doesn't trigger any Javascript function.

Sandstedt
11-16-2012, 05:04 AM
That should work, but why are you using an array, IE(0)? Only a single IE object should be needed -

IE.Document.getElementById("list").SelectedIndex = 2
- unless you are using an array of InternetExplorer objects. Post your code in full if you want more help.

I was working with several IE objects earlier, hence the array.


No, looking at the HTML source, there is no onclick or other event handler in the <select id="list" ...> element, so clicking it manually or by VBA doesn't trigger any Javascript function.

Ok, how does the website react to my mouseclick? I don't understand why the website won't reload the page when my VBA code changes the listbox value.

Sandstedt
11-16-2012, 05:52 AM
In the HTML source, there are some connections to the javascript(?) file steal.production.js

I opened this file in Chrome and created event listener breakpoints, and then manually changed the dropdown list value. This activated the "change" control, and led me to the following code in the .js-file:


a:a.replace(B,"mouseenter$1 mouseleave$1")};h.event={add:function(b,c,d,e,f){var g,k,l,j,s,n,m,p,o;if(!(b.nodeType===3||b.nodeType===8||!c||!d||!(g=h._data( b)))){if(d.handler){m=d;d=m.handler}if(!d.guid)d.guid=h.guid++;l=g.events;i f(!l)g.events=l={};k=g.handle;if(!k){g.handle=k=function(b){return typeof h!=="undefined"&&(!b||h.event.triggered!==b.type)?h.event.dispatch.apply(k.elem,arguments):a };k.elem=b}c=h.trim(fa(c)).split(" ");for(g=0;g<c.length;g++){j=ba.exec(c[g])||[];s=j[1];n=(j[2]||"").split(".").sort();

Is it safe to say then that there is a javascript function at work when I change the dropdown list?

mohanvijay
11-16-2012, 07:39 AM
below code works fine for me


IE.Document.getElementById("list").selectedIndex = 8
IE.Document.getElementById("list").FireEvent ("onchange")

Sandstedt
11-16-2012, 09:39 AM
below code works fine for me


IE.Document.getElementById("list").selectedIndex = 8
IE.Document.getElementById("list").FireEvent ("onchange")


That's great! I wonder why my code fails. The dropdown list changes value but the onchange-event doesn't trigger anything. Can you find any errors?


Sub UpdateList()
Dim IE As Object

lng_Sleep = 500

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
apiShowWindow IE.hwnd, SW_SHOWMINIMIZED
Sleep lng_Sleep
IE.navigate "https://www.avanzabank.se/aktier/lista"

Do
DoEvents
Sleep lng_Sleep
Loop While IE.ReadyState <> READYSTATE_COMPLETE

IE.Document.getElementById("list").selectedIndex = 8
IE.Document.getElementById("list").FireEvent ("onchange")
End Sub

I use IE9, if it's any help.

Sandstedt
11-16-2012, 03:36 PM
Solved!

IE9 was the problem. Reverted to IE8, and modified the code to:

ie.document.getElementByID("list").Focus
ie.document.getElementByID("list").selectedIndex = 8
ie.document.getElementByID("list").FireEvent ("onchange")

Didn't work without the .Focus-line. No idea why. Thanks everyone who tried to help.

Rafouf
04-15-2020, 02:04 AM
Hello everyone,

I have the exact same problem with my code, a drop down conditionned by the drop down "PRODUCTID" doesn't update.
But "focus" + "onchange" doesn't work...

Here is my HTML code :



<select data-mandatory="true" data-validation-type="3" class="field " name="PRODUCTID" data-field-name="engineModel" value=""> <option value="">Select</option>
<option>CFM56-2</option>
<option>CFM56-3</option>
<option>CFM56-5A</option>
<option>CFM56-5B</option>
<option>CFM56-5C</option>
<option>CFM56-7B</option>
<option>LEAP-1A</option>
<option>LEAP-1B</option>
</select>


And my VBA code :


For Each engine In IE.Document.GetElementsByName("PRODUCTID")(0).GetElementsByTagName("option")
If engine.innerText = "LEAP-1A" Then
engine.Focus
engine.Selected = True
engine.FireEvent ("onchange")
End If
Next



Thanks a lot in advance (here is my first post ever !)

macropod
04-15-2020, 09:48 PM
Rafouf: This thread was last active in November 2012. Instead of trying to resurrect such an ancient thread, kindly start a new one. Thread closed.