Working on an automation test using codeceptjs/selenium, I need to read the text of the nodes of a select drop down without knowing what they will be ahead of time and excluding the greyed out default option (since the app will not function until a choice is made).
<select id="itemname1">
<option class="greydefault">How Many?</option>
<option>6</option>
<option>8</option>
<option>10</option>
<option>12</option>
</select>
<select id="itemname2">
<option class="greydefault">What Type?</option>
<option>a</option>
<option>b</option>
<option>c</option>
<option>d</option>
</select>
Given the above simple dom, question is how to write an xpath that grab all 4 of the option nodes that contain 6 8 10 and 12. And another xpath that returns all 4 of the option nodes a b c and d.
Here are my attempts that don't work for me:
.//option/preceding::option[text()="What Type?"]/following::option[text()="How Many?"]
.//option/following::option[text()="How Many?"]/preceding::option[text()="What Type?"]
.//option/preceding::option[text()="What Type?"] and following::option[text()="How Many?"]
.//option/[preceding::option[text()="What Type?"] and following::option[text()="How Many?"]]
The first 2 attempts returns only the node "How many" for me, and the next attempts are invalid.
The output of the xpath query should return each option node other than the default node.
That xpath is put into a function that reads the texts of the nodes, it looks like this:
getElementsText(locator) {
let driver = this.helpers.Protractor.browser;
return driver.element.all(locator).getAttribute("textContent").then((result) => {
return result.toString().split(',');
});
}