javascript - 使用赛普拉斯中的索引访问 p 下拉项目
问题描述
我有一个p-dropdown
:
HTML:
<span>
<p-dropdown formControlName="theatreGroup" [options]="theatreGroupsList">
</p-dropdown>
</span>
TS:
theatreGroupsList: any[] = [
{ label: 'Hamlet', value: 100 },
{ label: 'Dutchman', value: 351 },
{ label: 'King Lear', value: 180 },
{ label: 'Candida', value: 211 },
{ label: 'Twelfth Night', value: 133 }
];
我需要能够获取 theatreGroupsList 并选择一个项目。我可以通过检查数组中项目的值来做到这一点:
cy.get('p-dropdown[formControlName="theatreGroup"]').click().contains('Candida').click();
但问题是 theatreGroupsList 是动态的。因此,我需要能够随时检索列表并使用索引(即不是值或标签)访问其元素。你能帮我解决这个问题吗?
解决方案
我受到 Steve Zodiac 的评论和 KKhan 的回答的启发,并开发了适合我的解决方案:
cy.get('p-dropdown[formControlName="theatreGroup"]').click().then(x => {
cy.get('p-dropdown[formControlName="theatreGroup"]>div>div>div>ul>p-dropdownitem').then(groups => {
// Assume we need to access item at index 3, then select in the dropdown
let group3 = groups[3]['innerText'];
// An extra click to prevent error about detached element from the DOM.
cy.get('p-dropdown[formControlName="theatreGroup"]').click();
cy.get('p-dropdown[formControlName="theatreGroup"]').click().get('div').contains(group3).click();
});
});
推荐阅读
- vue.js - 如何更新 vuetify mdi 图标?(Nuxt.js)
- r - Roxygen 文档:图片未显示
- java - Appium:ClassNotFoundException:org.openqa.selenium.remote.internal.ApacheHttpClient$Factory
- c++ - 在 OpenGL 和 SDL 中使用多线程
- mysql - 这些 sql 表中的平均 year_max - year_min 是多少?
- dom - 如何检查 DOM .contains?在clojurescript中?
- sql - 创建行满足条件的 Impala 文本表
- python - Django DetailView 模板未显示详细信息
- amazon-web-services - AWS S3 的 put 对象的 getSignedUrl 返回 403
- angular - 如何从异步函数更新 HTML?