Jump to content

Browser automation for 'select2' dropdown (search and select)


MilenP
 Share

Recommended Posts

Hello guys,


I want your advise here. I have a select2 dropdown menu that have to make automation on. I have tried with:

;$jQuery('#s2id_company_id').select2("val", "16013").trigger('change.select2');

but it is not working correctly... It is selecting the value, but there is something like AJAX that is making changes after selecting something from the dropdown, Example:

 

image.png.cf69fb678496410cc34adb3436248445.png

 

after:

Automatically changed:

 

image.png.1147f2eb708e98b22a0482d3e7192d30.png

 

You can see the html of it below:

 

image.thumb.png.fc8879e8d8e0ac57d52cccc1bab30cae.png

 

Currently in order to work I am doing the following:

 

$jQuery('#s2id_company_id').select2('search', $CompanyName)         ; Search by Company
        Sleep(2000)
        _ClickSearchedValueSelect2()
        Sleep(1500
        
        
        
        
    Func _ClickSearchedValueSelect2()

    ControlSend("Terminal", "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "{ENTER}")
    ControlSend("Terminal", "", "[CLASS:Internet Explorer_Server; INSTANCE:9]", "{ENTER}")

    Sleep(1500)

    ControlSend("Terminal", "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "{TAB}")
    ControlSend("Terminal", "", "[CLASS:Internet Explorer_Server; INSTANCE:9]", "{TAB}")

EndFunc   ;==>_ClickSearchedValueSelect2

 

The above is working, but when the automation using the ControlSend is working, who is using it will not be 100% able to work on the PC because the focus from the windows they will be working on will be taken in some point.

Can someone advise how is the best way of doing the select2 item selection with triggering the changes after it ?

 

Thank you guys,

 

Milen

 

Link to comment
Share on other sites

The dropdown I have issues with is exactly like this one:
image.png.f9392f5c4067f4f36640742746cce2c1.png

 

but unfortunately I cannot reproduce the issue from the website you provided, because the script just hang when try to.

What I use now is 

$jQuery('#s2id_company_id').select2('search', $CompanyName)         ; Search by Company
        Sleep(2000)
        _ClickSearchedValueSelect2()
        Sleep(1500
        
        
        
        
    Func _ClickSearchedValueSelect2()

    ControlSend("Terminal", "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "{ENTER}")
    ControlSend("Terminal", "", "[CLASS:Internet Explorer_Server; INSTANCE:9]", "{ENTER}")

    Sleep(1500)

    ControlSend("Terminal", "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "{TAB}")
    ControlSend("Terminal", "", "[CLASS:Internet Explorer_Server; INSTANCE:9]", "{TAB}")

EndFunc   ;==>_ClickSearchedValueSelect2

and it is working, but I want to start doing it the way you suggested:

 

$jQuery('#s2id_company_id').select2("val", "16013", "triggerChange", true)

which is working for the fields without minimumInputLength

Thank you again for your assistance, much appreciated!

 

Link to comment
Share on other sites

Hi again @Danp2,

 

I have reproduced it form the site you gave me, you can see the example code below:

 

#include <IE.au3>


Global $oIE = _IEAttach("http://select2.github.io/select2/", "url")

Local $jQuery = _jQuerify($oIE)

$jQuery('#s2id_e3').select2("val", "Alaska", "triggerChange", true)

 

The above code is working, it is changing the value to Alaska, when it is something else, for example Oregon. But, when it is Alaska, I cannot change ti to anything else.

 

#include <IE.au3>


Global $oIE = _IEAttach("http://select2.github.io/select2/", "url")

Local $jQuery = _jQuerify($oIE)

$jQuery('#s2id_e3').select2("val", "Oregon", "triggerChange", true)

The above is not changing to Oregon when it is on something else, Alaska for example.

 

Maybe there is some other way to work with this kind of select2 fields with minimumInputLength  set, I hope you will advise me what to do :)

Thank you,

Milen

 

Link to comment
Share on other sites

You are using the wrong values. Try this --

#include <IE.au3>

Global $oIE = _IECreate("http://select2.github.io/select2/", 1)
Local $jQuery = _jQuerify($oIE)

$jQuery('#s2id_e3').select2("val", "OR", "triggerChange", true)
Sleep(1000)
$jQuery('#s2id_e3').select2("val", "FL", "triggerChange", true)
Sleep(1000)
$jQuery('#s2id_e3').select2("val", "TN", "triggerChange", true)

 

Link to comment
Share on other sites

  • 2 weeks later...

Hi @Danp2,

 

I have asked the website admins about the select2 dropdowns I have issues with, and they told me that:

 

Quote

Is because the second one uses a dynamic search (where the results are fetched from the server based on user input), whereas the first one has all the results preloaded (which is why it's so slow). So you can manually change the value of the pre-loaded version but not the dynamic load version which requires "searching".

 

 

My question to them was why on some of their select2 dropdowns I can directly select with

$('#s2id_e3').select2("val", "Alaska", "triggerChange", true)

but on another I can't and as a workaround I am using 

 

$jQuery('#s2id_company_id').select2('search', $CompanyName)         ; Search by Company
        Sleep(2000)
        _ClickSearchedValueSelect2()
        Sleep(1500
        
        
        
        
    Func _ClickSearchedValueSelect2()

    ControlSend("Terminal", "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "{ENTER}")
    ControlSend("Terminal", "", "[CLASS:Internet Explorer_Server; INSTANCE:9]", "{ENTER}")

    Sleep(1500)

    ControlSend("Terminal", "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "{TAB}")
    ControlSend("Terminal", "", "[CLASS:Internet Explorer_Server; INSTANCE:9]", "{TAB}")

EndFunc   ;==>_ClickSearchedValueSelect2

 

@Danp2, as you have a lot of experience with similar things, can you please advise me if really there is a better way of selecting the values when they are fetched from the server based on user input.

 

Thank you,

Milen

 

Link to comment
Share on other sites

Unless you can find another website that allows us to observe this behavior, then the only suggestion I would give is to use the debugging tools in Firefox or Chrome to try to identify a way to accomplish your goal using javascript. If you find a way, then you can generally convert that into something usable in Autoit.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...