简体   繁体   English

如何检查 puppeteer 中元素句柄上的 disabled 属性?

[英]How to check the disabled property on an element handle in puppeteer?

I am having the code as given below:我的代码如下:

let btns = await page.$$(".move-to-preview-env");
let addToRepl = await btns[0];
let moveBtn = await btns[1];

console.log('addtoRepl ', addToRepl) // I want to check if these buttons are enabled or disabled ??
console.log('moveBtn ', moveBtn)

I also tried this:我也试过这个:

let btns = await page.$$('button[disabled]')
let btn1 = await btns[0].disabled;
let btn2 = await btns[1].disabled;
console.log('btns ', btns)   // giving array of element handle
console.log('btn1', btn1)   // giving undefined ???

Whereas I tried this in the browser console and giving correct result:而我在浏览器控制台中尝试了这个并给出了正确的结果:

let ele = document.querySelectorAll('button[disabled]');
ele[0].disabled  // true

But not getting disabled/enabled property using puppeteer.但没有使用 puppeteer 获得禁用/启用的属性。

It's virtually the same as in Puppeteer, and here on SO is the same question.它与 Puppeteer 中的几乎相同, 在 SO上也是同样的问题。

To check whether a button is disabled:检查按钮是否被禁用:

const isDisabled = await page.$('button[disabled]') !== null;

or with page.$eval() :或使用page.$eval()

const isDisabled = await page.$eval('button', btn => btn.disabled);

To check all buttons are disabled:要检查所有按钮是否被禁用:

const disabledButtons = (await page.$$('button[disabled]')).length;
const buttons = (await page.$$('button')).length;
// now assert the two numbers are equal

Or to check a button is enabled:或者检查一个按钮是否启用:

const isEnabled = await page.$('button:not([disabled])') !== null;

There are more solutions, these are just some examples.还有更多解决方案,这些只是一些示例。 I recommend reading the linked SO question.我建议阅读链接的 SO 问题。

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

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