首页 > 解决方案 > 如何在量角器中使用 formcontrolname 从下拉列表中选择值?

问题描述

<select _ngcontent-c1="" class="form-control ng-untouched ng-pristine ng-invalid" formcontrolname="Designation" required=""><option _ngcontent-c1="" value="">Select Designation</option><!----><option _ngcontent-c1="" value="CEO">CEO</option><option _ngcontent-c1="" value="GM">GM</option><option _ngcontent-c1="" value="BodyGuard">BodyGuard</option><option _ngcontent-c1="" value="Executive">Executive</option></select>

对于上面的 html,我正在尝试使用量角器从下拉列表中选择值。尝试以下但不工作。

var EmpDesignation = element(by.cssContainingText('body.modal-open:nth-child(2) modal-container.modal.fade.show:nth-child(7) div.modal-dialog div.modal-content form.form-horizontal.ng-pristine.ng-invalid.ng-touched div.modal-body div.form-row:nth-child(2) div.col-md-10 > select.form-control.ng-pristine.ng-invalid.ng-touched:nth-child(3)', 'CEO'));
EmpDesignation.click();

错误:失败:使用定位器找不到元素:by.cssContainingText("body.modal-open:nth-child(2) modal-container.modal.fade.show:nth-child(7) div.modal-dialog div.模态内容 form.form-horizo​​ntal.ng-pristine.ng-invalid.ng-touched div.modal-body div.form-row:nth-child(2) div.col-md-10 > select.form-control .ng-pristine.ng-invalid.ng-touched:nth-child(3)", "CEO")

有多个类名称为“form-control ng-untouched ng-pristine ng-invalid”。

任何人都可以提出解决办法吗?我们可以使用 formcontrolname 标签吗?

标签: protractor

解决方案


我会这样尝试

let EmpDesignation = element(by.xpath('//select[@formcontrol="Designation"]'));

EmpDesignation.sendKeys('CEO'); //Option 1
EmpDesignation.element(by.cssContainingText('option','CEO')) //Option 2

然而,这确实假设它formcontrol="Designation"是独一无二的。如果该假设不正确,请告诉我,我会更新。


推荐阅读