Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
558 views
in Technique[技术] by (71.8m points)

Angular Cypress how to get elements which has got dynamic ids

I am completely new to E2E with Cypress. I have a table with dynamic rows. I want to shift+select particular column in all the rows and copy paste a value.

Can anyone help me how to get the dynamic element. in this example i have 108 rows and i want to select 3rd column of each row. The column has to following id. and i want to change value 'No' to 'Yes' in this example.

<div class="wj-cell wj-alt wj-wrap editable-cell" tabindex="-1" role="gridcell" aria-required="true" id="catRatingImportManualInputRow105Col3" style="left: 610px; top: 3485px; width: 155px; height: 33px;">
    <button class="wj-btn wj-btn-glyph wj-right wj-elem-dropdown" type="button" tabindex="-1" aria-label="Toggle Dropdown" aria-expanded="false">
        <span class="wj-glyph-down"></span>
    </button>
    No
</div>

Thanks

question from:https://stackoverflow.com/questions/65849030/angular-cypress-how-to-get-elements-which-has-got-dynamic-ids

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use each() to loop through your table in cypress. to access the third element you can use the index value.

Since your id is catRatingImportManualInputRow105Col3 is dynamic, so we will use only use the part of your id that is static, so your css selector should be something like: div[id*="catRatingImportManualInputRow"]

cy.get('div[id*="catRatingImportManualInputRow"]').each(($ele, index) => {
    if (index == 2)
        //Do something with the third element
        cy.wrap($ele).click().type('Yes')
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...