YasserKhalil
YasserKhalil

Reputation: 9538

Select an item from drop down in IE

In the following picture I am trying to click the option 50 from this drop down menu which is implemented in a table in html structure Any help please enter image description here

I tried this line

.document.getElementByName("WatchedCompaniesGrid_length").Value = "50"

But this throws an error

I also tried those lines

     Set oElt = ie.document.getElementsByName("WatchedCompaniesGrid_length")
If Not oElt Is Nothing Then
    oElt(0).Value = "50"

End If

and it actually selects the option but doesn't trigger the event .. I tried oElt.FireEvent "onchange" but doesn't work for me

This is html lines

<input name="WatchedCompaniesGridSelectedId" id="WatchedCompaniesGridSelectedId" type="hidden" value="" autocomplete="off"><script language="javascript" type="text/javascript">WatchedCompaniesGridinit = new RegSysPortal.Grid();WatchedCompaniesGridinit.Initialise('WatchedCompaniesGrid','','','','','','', '', '', 'false', 'true', 'true', 'true', 'two_button', '5', 'asc', '', '10', '1,R,3,C,4,C,5,C,6,C,7,C,8,C', '0', '0', '', '0', null, false, false);</script><div class="frmResponse_blank" id="WatchedCompaniesGrid_result"><span class="frmResponse_message_span"></span></div>    <div class="dataTables_wrapper" id="WatchedCompaniesGrid_wrapper" role="grid"><div class="fg-headerbar"><div class="dataTables_length" id="WatchedCompaniesGrid_length"><span><table><tbody><tr><td>Show </td><td><select name="WatchedCompaniesGrid_length" aria-controls="WatchedCompaniesGrid" size="1"><option value="5">5</option><option value="10">10</option><option value="25">25</option><option value="50">50</option><option value="100">100</option></select></td><td>entries</td></tr></tbody></table></span></div><div class="dataTables_filter" id="WatchedCompaniesGrid_filter"><span><table><tbody><tr><td>Search:</td><td><input aria-controls="WatchedCompaniesGrid" type="text" autocomplete="off"></td></tr></tbody></table></span></div></div><table class="z-index: 2000 dataTable" id="WatchedCompaniesGrid" aria-describedby="WatchedCompaniesGrid_info" style="width: 987px;">

** this is html part

<table><tbody><tr><td>Show </td><td><select name="WatchedCompaniesGrid_length" aria-controls="WatchedCompaniesGrid" size="1"><option value="5">5</option><option value="10">10</option><option value="25">25</option><option value="50">50</option><option value="100">100</option></select></td><td>entries</td></tr></tbody></table>

Upvotes: 2

Views: 444

Answers (2)

SIM
SIM

Reputation: 22440

Try this. I used hardcoded delay to let the browser update it's content. The selector is able to select and change the desired number from dropdown. However, the only porblem was that the new content never updated unless we opted for keyboardevent.

Dim Html As HTMLDocument, post As Object, , evt As Object

Set Html = IE.document ''without this line queryselector fails sometimes

Application.Wait Now + TimeValue("00:00:03")

Set post = Html.querySelector("select[name='WatchedCompaniesGrid_length']")
post.selectedIndex = 3

This is the portion that made that webpage update the result. I tried to show how you can use it in your script.

Set evt = Html.createEvent("keyboardevent")
evt.initEvent "change", True, False

Set post = Html.querySelector("select[name='WatchedCompaniesGrid_length']")
post.selectedIndex = 3
post.dispatchEvent evt

Upvotes: 2

BLitE.exe
BLitE.exe

Reputation: 321

First thing first what version of IE are you using? Cause it will depend on what is the event trigger for this selection.

You can try this to select the index of that selection based on the option value.

Set oElt = ie.document.getElementsByName("WatchedCompaniesGrid_length")
    For a = 0 To oElt.Options.Length - 1
        If oElt.Options(a).Text = "sample Option Value" Then
            oElt.selectedIndex = a
            Exit For
        End If
    Next

Now for the event trigger part. After you select your desired option.

In IE11 you can try this one.

Dim objEvent
    Set objEvent = IE.Document.createEvent("HTMLEvents")    'Create an event handler for IE.Document
    objEvent.initEvent "change", False, True
    oElt.dispatchEvent objEvent   

Upvotes: 3

Related Questions