Stealth Rabbi
Stealth Rabbi

Reputation: 10346

Getting an md-select option list in selenium

I have a md-select dropdown WebElement. I can get it's md-options by doing:

List<WebElement> dropDownElements = myDropDown.findElements(By.tagName("md-option"));`

This gets me a list of md-options. The list has my expected number of items. However, I want to verify the text in these md-options are correct. The value attribute of each of those WebElements is [object Object], and I want the actual text.

By traversing the fields of the web element, I see that the firstChild_ of the firstChild_ 's data_ field has the actual text i'm looking for (with a lot of white space padding). It's in a DomText object. But, the API doesn't have any kind of getChild(), and I think that's clunky anyways?

How can I easily get to the text contents of the md-options? Would xpath allow me to get what I want? How would I do that query?

HTML:

<md-input-container class="my-container" flex-gt-sm>
    <label>Some Label</label>
    <md-select ng-model="myCtrl.selectedItem" required id="myDropDown">
        <md-option ng-value="selectedItem"
                   ng-repeat="selectedItem in myCtrl.itemList">
            {{selectedItem.thing1 + ': ' + selectedItem.thing2}}
        </md-option>
    </md-select>
</md-input-container>

Upvotes: 0

Views: 1414

Answers (1)

Stealth Rabbi
Stealth Rabbi

Reputation: 10346

I created this helper method to get teh string values for a given mdSelect

public static List<String> getStringsInMdSelect(WebElement mdSelect) {
    List<String> selectionStrings = new ArrayList<>();
    WebElement contentElement = mdSelect.findElement(By.tagName("md-content"));
    List<WebElement> contentDivs = contentElement.findElements(By.tagName("div"));

    for (WebElement contentDiv : contentDivs) {
        String rawText = contentDiv.getAttribute("textContent");
        selectionStrings.add(rawText.trim());
    }
    return selectionStrings;
}

Upvotes: 2

Related Questions