Select2 is a jQuery based replacement for select boxes. This article will demonstrate how Selenium webdriver can handle Select2 by manipulating the first such selection box in the Examples page of Select2.
In [3]:
import os
from marigoso import Test
request = {
'firefox': {
'capabilities': {
'marionette': False,
},
}
}
Note that in order for the extensions to be installed in the browser, you need to either specify an extension enabled Firefox profile to Selenium or you specify the location and name of Firefox extensions you want to install. In the above example, I have Firebug and FirePath files stored in 'tools\firefox' folder so I can just specify the location and filenames of the extensions.
In [ ]:
browser.get_url('https://select2.github.io/')
browser.press("Examples")
Firebug will then display and highlight the HTML source of the Selection Box as well as highlight the control itself if you hover your mouse to the HTML source.
We now have the task of figuring out what locator we can use to locate this Selection Box. The Selection Box is a 'span' element with an id="select2-jnw9-container", we can surely make use of this id attribute. However, it appears that this id is randomly generated so I made a slight modification to make sure my locator will still work even if the page is refreshed.
Firebug will now display the same thing as before, but notice now that at the lower left part of Firebug window it says '17 matching nodes'. This means we have 17 such Selection Box that can be located using my chosen selector. However, this time we are only interested on the first Selection Box, so I think my chosen selector is still useful.
The ultimate way to verify that the locator works is to feed it to Selenium and run it. So we execute the following command.
In [4]:
browser.press("css=[id^='select2']" )
If the Selection Dropdown appears upon executing the above command, then we are on the right track. You can run the above command several times to confirm the closing and opening of the selection dropdown.
We now need to identify the locator for the Selection Dropdown. We do this by clicking back on the 'HTML' tab in the Firebug window and observing that when you manually click on the Selection Box another 'span' element is dynamically being added at the buttom of the HTML source.
We can use previous technique of locating the Selection Box above to arrive to a conclusion that the locator for Selection Dropdown could be 'css=span.select2-dropdown > span > ul'. Note that in this case we specifically located until the 'ul' tag element. This is because the options for Select2 are not 'option' tag elements, instead they are 'li' elements of a 'ul' tag.
In [5]:
browser.select_text("css=*[id^='select2']", "Nevada", 'css=span.select2-dropdown > span > ul')
In [6]:
browser.select2("css=*[id^='select2']", 'css=span.select2-dropdown > span > ul', "Hawaii")
In [7]:
import os
from marigoso import Test
request = {
'firefox': {
'extensions_path': os.path.join(os.getcwd(), 'tools', 'firefox'),
'extensions': ['firebug@software.joehewitt.com.xpi', 'FireXPath@pierre.tholence.com.xpi'],
}
}
browser = Test(request).launch_browser('Firefox')
browser.get_url('https://select2.github.io/')
browser.press("Examples")
browser.select_text("css=*[id^='select2']", "Nevada", 'css=span.select2-dropdown > span > ul')
browser.quit()