How to select the text of internal elements in GEB?
I have the following script:
<div>
<ul class="select2-results" style="width: 400px;">
<li class="select2-results-dept-0 select2-result select2-result-selectable">
<div class="select2-result-label">
<span id="12345" class="null">
GBP
</span>
</div>
</li>
<li class="select2-results-dept-0 select2-result select2-result-selectable">
<div class="select2-result-label">
<span id="45678" class="null">
KPW
</span>
</div>
</li>
<li class="select2-results-dept-0 select2-result select2-result-selectable">
<div class="select2-result-label">
<span id="12345" class="null">
USD
</span>
</div>
</li>
</ul>
</div>
I need to choose a currency from it. I tried the following, but in no way:
assert $('UL.select2-results LI.select2-results-dept-0.select2-result.select2-result-selectable', 0..2)*.text() == ["GBP", "KPW", "USD"]
Even I could not handle it:
assert $('UL.select2-results LI.select2-results-dept-0.select2-result.select2-result-selectable')*.size() == 3
Although, I can handle one element:
assert $('UL.select2-results').find("LI.select2-results-dept-0.select2-result.select2-result-selectable", 0).text() == "GBP"
Any help or suggestion would be really helpful for me! Thank!
I don't know why @Gabriel's answer is not working! I know that there was a problem with 0..3, since that would be 0..2! But instead of finding it, it finally worked:
assert $('UL.select2-results LI.select2-results-dept-0.select2-result.select2-result-selectable span')*.text() == ["GBP", "KPW", "USD"]
I would really like to know if anyone can explain why indexing + with find + * does not work together for this particular problem or if there is any problem with the logical syntax.
, 3 DIV s. , , .find('span') .
assert $(
'UL.select2-results LI.select2-results-dept-0.select2-result.select2-result-selectable',
0..3)*.text() == ["GBP", "KPW", "USD"]
assert $(
'UL.select2-results LI.select2-results-dept-0.select2-result.select2-result-selectable',
0..3).find('span')*.text() == ["GBP", "KPW", "USD"]
I think you could simplify how you get currencies by doing the following:
$("div.select2-result-label").children("span")*.text() == ["GBP", "KPW", "USD"]
Since you already have classes in the div, you can easily access them using the selector above, then you can get the span elements for the children and get the text of each range. Hope this helps!