问题
I'm trying to select the third option in a dropdown list. Below are some details about the element:
Outer HTML:
<md-option _ngcontent-c6="" role="option" ng-reflect-value="last90Days" tabindex="0" id="md-option-2" aria-selected="false" aria-disabled="false" class="mat-option"><!--bindings={
"ng-reflect-ng-if": "false"
}--> Last 90 Days <!--bindings={
"ng-reflect-ng-if": "true"
}--><div class="mat-option-ripple mat-ripple" md-ripple="" ng-reflect-trigger="[object HTMLElement]"> </div> </md-option>
CSS Selector:
#md-option-37 > div:nth-child(1)
Sorry for the terrible formatting. If anyone has any suggestions about how to select the "Last 90 Days" dropdown item then I'd be very grateful.
回答1:
Based on what I understand out of your info an the AngularJS md-select
you need to do the following
// Open the md-select, use the correct selector, this is a demo
$('md-select').click();
// There is an animation, I don't know how long, you need to wait for the animation to be finished.
// Quick and dirty is a browser.sleep(), but it's better to find a more stable way because if the animation will take longer in the future your test will break
browser.sleep(500);
// Click on you option, this can be done in several ways
// By index
$$('md-option').get(1).click();
// By text
element(by.cssContainingText('md-option', 'your text')).click();
// Wait for the menu to close, this is also an animation
browser.sleep(500);
For element(by.css('selector'));
I always use the shorthand notation, for the rest of the selectors see the docs of Protractor.
I would advice to use a better way to wait for the animation to be done. Here I used a browser.sleep()
, but it's not future proof. You don't want your script to rely on sleeps.
Hope it helps.
来源:https://stackoverflow.com/questions/44073870/trouble-locating-angular-element-for-protractor-ui-nav-test