简体   繁体   English

下拉元素未定位

[英]The drop down element is not locating

Requirement : Click on the drop down and the drop down should open .要求:点击下拉菜单,下拉菜单应打开

DOM: 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>

The element is located on the UI:该元素位于 UI 上:
But when I use the same id on the code as below:但是当我在下面的代码中使用相同的 id 时:

cy.get('#select2_ID-container').click({force:true})

Then I get the following error:然后我收到以下错误:

Timed out retrying: Expected to find element: #select2_ID-container, but never found it.

I also tried {force: true} :我也试过{force: true}

cy.get('#select2_ID-container').click({force:true})

上面显示了一个不同的 id,也许你想要

cy.get('[id="select2_ID-container"]').click()

也许您正在寻找role="combobox" ,因为这很可能是下拉菜单。

cy.get('span[role="combobox"]').click({force:true})

#select2_ID-container is the selector for the first option in the dropdown list which is Choose Inco Term . #select2_ID-container是下拉列表中第一个选项的选择器,即Choose Inco Term You can use this to open the drop down.您可以使用它来打开下拉菜单。

cy.get('[aria-owns="select2_ID-results"]').click()
OR
cy.get('[aria-activedescendant="select2_ID-result-pwlg-2"]').click()

Or, You can also use the text to find and click.或者,您也可以使用文本查找并单击。

cy.contains('Choose Customer').click()

The jQuery select2 has a visible textbox which can be clicked to show the options. jQuery select2 有一个可见的文本框,可以点击它来显示选项。

If you're having trouble with the ID, this is how I would approach the test如果您在使用 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

Sometimes cypress requires a mouse move.有时 cypress 需要鼠标移动。 Try this too:也试试这个:

cy.get('[id="select2_ID-container"]').trigger('mousemove').click()

Also make sure the element is present / not timedout by checking the command logs : https://docs.cypress.io/api/commands/click#Command-Log还要通过检查命令日志确保元素存在/未超时: https ://docs.cypress.io/api/commands/click#Command-Log

Finally I found a solution to this.最后我找到了解决方案。 I tried all of the above solutions, but neither worked for me.我尝试了上述所有解决方案,但都不适合我。 This was due to the version issue.这是由于版本问题。 I simple updated the Cypress to the latest version 10.0.1 and ran the test and it worked.我简单地将 Cypress 更新到了最新版本 10.0.1 并运行了测试,它工作正常。 Also the dropdown is not located because the page was not loaded properly.此外,由于页面未正确加载,因此未找到下拉列表。 The click action was performed on the automation before the page loads completely.单击操作是在页面完全加载之前对自动化执行的。 So I added cy.wait(10000) before clicking the dropdown.所以我在单击下拉列表之前添加了 cy.wait(10000) 。 I think the version is not the main problem.我认为版本不是主要问题。 The main problem is the page load.主要问题是页面加载。 Thank you all for your time.谢谢大家的时间。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM