赛普拉斯-cy.click()不会在select2下拉列表中单击值 [英] Cypress - cy.click() does not click on the value in the select2 drop down

查看:35
本文介绍了赛普拉斯-cy.click()不会在select2下拉列表中单击值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有select2类的下拉列表.单击下拉菜单中的箭头后,我得到以下在DOM中激活的代码,该代码在下拉菜单中具有值 Reason1 Reason2 .我想迭代可用的值,然后选择所需的选项.

I have a dropdown which has select2 class. On clicking the arrow in drop down, I get below code activated in DOM which has the values Reason1 and Reason2 in the dropdown. I want to iterate on the available values and select the desired option.

使用每个函数,我都可以在数组上进行读写并获取可用的值,但是我无法对同一函数执行cy.click()来获取所选的值.我没有收到任何错误,并且代码运行正常,但是单击没有发生(在div,li或span标记上都没有)

Using each function, I am able to itearte on the array and get the available values, but I am unable to perform cy.click() on the same to get the value selected. I am not getting any error and code is running fine but the click in not happening (neither on div , li nor span tag)

`<ul class="select2-results">
  <li class="select2-results-dept-0 select2-result select2-result-selectable">
     <div class="select2-result-label">
       <span class="select2-match"></span>
        Reason1
     </div>
 </li>
   <li class="select2-results-dept-0 select2-result select2-result-selectable">
     <div class="select2-result-label">
       <span class="select2-match"></span>
       Reason2
     </div>
  </li>
</ul>`

下面是我的代码

    cy.get('#select2-drop > .select2-results').find('li').each(($el,index,$list) => {  
    let option = $el.find('div').text()

    if(option=='Reason2')
    {                
      $el.find('div').click()
    }
 })

如果在if条件中添加 $ el.find('div').css('background-color','yellow'),元素的背景色将变为黄色,但是单击没有发生.

If I add $el.find('div').css('background-color','yellow') in the if condition, elements background color is getting changed to yellow but click is not happening.

任何人都可以帮忙.谢谢!

Can anyone please help on this. Thanks!

推荐答案

如果要选择第二个,则可以使用以下命令

you can use these command if you want to select the second one

cy.get('#select2-drop > .select2-results')
    .type("{downarrow}")
    .type("{downarrow}")
    .type("{enter}")

这篇关于赛普拉斯-cy.click()不会在select2下拉列表中单击值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆