Matt Brennan Matt Brennan - 11 months ago 58
jQuery Question

Gather an acceptable XPath or CSS selector for Selenium using Firepath, and FireBug from Select2

A website I'm designing tests for uses the Select2 plugin for drop-down lists and entry fields. I can manage to manually code the clicking and sendkeys functions for the initial fields, but drop-downs have me messed up. I can select the drop-down item, but not any sort of unique identifier that Selenium IDE will accept for quick testing.

How can I select one of the drop-down options?

<div id="select2-drop-mask" class="select2-drop-mask" style="display: block;"/>
<div id="select2-drop" class="select2-drop select2-display-none select2-drop-active select2-drop-above" style="left: 1015.5px; width: 438px; top: auto; bottom: -229px; display: block;">
<div class="select2-search select2-search-hidden select2-offscreen">
<span class="fa fa-search select2-search-placeholder"/>
<input class="select2-input" type="text" placeholder="Search" spellcheck="false" autocapitalize="off" autocorrect="off" autocomplete="off"/>
</div>
<ul class="select2-results">
<li class="select2-results-dept-0 select2-result select2-result-selectable select2-highlighted">
<div class="select2-result-label">
</li>
<li class="select2-results-dept-0 select2-result select2-result-selectable">
<div class="select2-result-label">
<span class="select2-match"/>
Fraudulent Account (ID Theft)
</div>
</li>


There are more list items, but I'd prefer to not give too many tells as to who this code is for. I would also prefer not to use any sort of numerical-array selectors, because the test code needs to be able to easily accept new amendments to the website.

Answer Source

You can match element by it's text value with following XPath:

//li[normalize-space(.)="Fraudulent Account (ID Theft)"]