要求:點擊下拉選單,下拉選單應打開。
DOM:
<span class="select2 select2-container select2-container--default select2-container--below select2-container--open" dir="ltr" data-select2-id="3522" style="width: 208.328px;" xpath="1">
<span class="selection">
<span class="select2-selection select2-selection--single" role="combobox" aria-haspopup="true" aria-expanded="true" tabindex="0" aria-disabled="false" aria-labelledby="select2-_ID-container" aria-owns="select2_ID-results" aria-activedescendant="select2_ID-result-pwlg-2">
<span class="select2-selection__rendered" id="select2_ID-container" role="textbox" aria-readonly="true" title="Choose Inco Term">Choose Inco Term</span>
<span class="select2-selection__arrow" role="presentation">
<b role="presentation"></b>
</span>
</span>
</span>
該元素位于 UI 上:
但是當我在代碼中使用相同的 id 時,如下所示:
cy.get('#select2_ID-container').click({force:true})
然后我收到以下錯誤:
Timed out retrying: Expected to find element: #select2_ID-container, but never found it.
我也試過{force: true}:
cy.get('#select2_ID-container').click({force:true})
uj5u.com熱心網友回復:
上面顯示了一個不同的 id,也許你想要
cy.get('[id="select2_ID-container"]').click()
uj5u.com熱心網友回復:
也許您正在尋找role="combobox",因為這很可能是下拉選單。
cy.get('span[role="combobox"]').click({force:true})
uj5u.com熱心網友回復:
#select2_ID-container是下拉串列中第一個選項的選擇器,即Choose Inco Term. 您可以使用它來打開下拉選單。
cy.get('[aria-owns="select2_ID-results"]').click()
OR
cy.get('[aria-activedescendant="select2_ID-result-pwlg-2"]').click()
或者,您也可以使用文本查找并單擊。
cy.contains('Choose Customer').click()
uj5u.com熱心網友回復:
jQuery select2 有一個可見的文本框,可以點擊它來顯示選項。
如果您在使用 ID 時遇到問題,這就是我將如何進行測驗
cy.get('.select2 [title="Choose Inco Term"]')
.as('select2') // alias the textbox
.click() // open the options list
// Options now open
cy.contains('li.select2-results__option', 'TextOfOption').click() // choose an option
// Verify
cy.get('@select2')
.find('li.select2-selection__choice') // choice is listed under textbox
.should('contain', 'TextOfOption') // check the text
// Remove
cy.get('@select2')
.find('.select2-selection__choice__remove') // remove button
.click()
cy.get('@select2')
.should('not.contain', 'TextOfOption') // check text has gone
uj5u.com熱心網友回復:
有時 cypress 需要滑鼠移動。也試試這個:
cy.get('[id="select2_ID-container"]').trigger('mousemove').click()
還要通過檢查命令日志確保元素存在/未超時:https ://docs.cypress.io/api/commands/click#Command-Log
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/484829.html
標籤:javascript html 用户界面 柏
下一篇:所選專案表面上的奇怪帶顏色
