I have a problem here where I am unable to locate the list items.
<ul id="select2-id_faculty_advisor-results" class="select2-results__options" role="tree" aria-multiselectable="true" aria-expanded="true" aria-hidden="false">
<li id="select2-id_faculty_advisor-result-0pu4-1" class="select2-results__option select2-results__option--highlighted" role="treeitem" aria-selected="true">Alice</li>
<li id="select2-id_faculty_advisor-result-cayw-2" class="select2-results__option" role="treeitem" aria-selected="false">Bob</li>
<li id="select2-id_faculty_advisor-result-4h8e-3" class="select2-results__option" role="treeitem" aria-selected="false">Candy</li>
<li id="select2-id_faculty_advisor-result-el4l-4" class="select2-results__option" role="treeitem" aria-selected="false">Dark</li>
</ul>
As seen above I'll not be able to find elements by id as the characters in between are generated dynamically. Any idea of how to locate a particular element??
Use the part of the id that does not change in a css selector like:
li[id*='faculty_advisor-result']
You can also add extra attributes if needed, for example aria-selected like:
li[id*='faculty_advisor-result'][aria-selected='false']
or any other attribute.
If you don't have any specific attribute for a particular option then you could use xpath to select the element that has that option like:
//li[contains(@id, 'faculty_advisor-result')][text()='Alice']
the same way you can add extra filters to narrow the results like [@role='tree']
the best way to do it make collection of list by xpath "//ul[@id='select2-id_faculty_advisor-results']/li"
after that you can select them by index from the collection.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.