如何在赛普拉斯中获取HTML属性值 [英] How to get HTML attribute value in Cypress

查看:51
本文介绍了如何在赛普拉斯中获取HTML属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与硒合作几年后,我开始学习赛普拉斯.在Selenium中,我经常使用GetAttribute()方法.作为练习,我试图对赛普拉斯执行相同的操作,以从以下HTML元素打印类属性值:

 < input class ="form-control ng-touched ng-pristine ng-valid"max ="21"min ="1"类型=数字". 

这是我的代码:

  cy.log(cy.get('input').invoke('attr','class')); 

输出:

 日志对象{5} 

我尝试使用Lakitna柏树命令(

解决方案

cy 命令是异步的,因此要进行日志记录,您需要使用 .then :

  cy.get('input').then(($ input)=> {cy.log($ input.attr('class'));}); 

 //带有断言cy.get('input').should('have.attr','class').then(cy.log); 

I am starting to learn Cypress after few years working with Selenium. In Selenium i'm regularly using GetAttribute() method. As an exercise i'm trying to do the same with Cypress, to print class attribute value from the following HTML element:

<input class="form-control ng-touched ng-pristine ng-valid" max="21" min="1" type="number">

This is my code:

cy.log(cy.get('input').invoke('attr', 'class'));

Output:

log Object{5}

I tried to use Lakitna cypress-commands (https://github.com/Lakitna/cypress-commands) with the code:

cy.log(cy.get('input').attribute('class'));

Output:

解决方案

cy commands are asynchronous so for logging you need to use .then:

cy.get('input').then(($input) => {
    cy.log($input.attr('class'));
});

or

// with assertion
cy.get('input').should('have.attr', 'class').then(cy.log);

这篇关于如何在赛普拉斯中获取HTML属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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