Utilizing protractor to interact with dropdown menus can be tricky, especially when the elements have dynamically changing IDs. To tackle this issue, I have been using element.all to retrieve text values and implementing if conditions to compare them against my desired selection in order to click on the correct item.
<div class="ng-trigger ng-trigger-transformPanel ng-tns-c4-15 mat-select-panel mat-primary ng-star-inserted" style="transform-origin: 50% 127.5px 0px; font-size: 14px; opacity: 1; min-width: calc(100% + 32px); transform: scaleY(1);">
<!---->
<mat-option _ngcontent-c20="" class="mat-option ng-star-inserted" role="option" tabindex="0" id="mat-option-59" aria-selected="false" aria-disabled="false">
<!---->
<span class="mat-option-text"> Cik </span>
<div class="mat-option-ripple mat-ripple" mat-ripple=""></div>
</mat-option>
<mat-option _ngcontent-c20="" class="mat-option ng-star-inserted" role="option" tabindex="0" id="mat-option-60" aria-selected="false" aria-disabled="false">
<!---->
<span class="mat-option-text"> Dato' </span>
<div class="mat-option-ripple mat-ripple" mat-ripple=""></div>
</mat-option>
<mat-option _ngcontent-c20="" class="mat-option ng-star-inserted" role="option" tabindex="0" id="mat-option-61" aria-selected="false" aria-disabled="false">
<!---->
<span class="mat-option-text"> Dato Paduka </span>
<div class="mat-option-ripple mat-ripple" mat-ripple=""></div>
</mat-option>
The approach I've taken is as follows:
function Select(title){
element(by.name("title")).click().then(function(){
browser.sleep(1000)
})
element.all(by.tagName("mat-option")).each(function(item){
item.getAttribute("id").getText().then(function(z){
if(z==title){
item.click().then(function(){
console.log("Successfully selected title");
})
}
})
});
However, a major hurdle I've encountered is that after clicking on an option, Protractor returns to the top and attempts to search for elements again. This leads to failures as the dropdown menu is already closed at this point. It fails to proceed to select other fields.
Below is the error message I receive post successful selection:
Failed: The element reference of <mat-option id="mat-option-575" class="mat-option ng-star-inserted"> is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed
Any suggestions on how to overcome this issue without encountering errors?