使用Puppeteer禁用检查元素 [英] Checking an element is disabled using Puppeteer

查看:64
本文介绍了使用Puppeteer禁用检查元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,其初始状态为 disabled -

I have a button that has an initial state of disabled -

<button type = "submit" class="ant-btn ant-btn-primary ant-btn-lg" disabled>

一旦满足条件,就不会显示 disabled 属性-因此HTML变为

The disabled attribute is not present once the conditions are met - so the HTML becomes

<button type = "submit" class="ant-btn ant-btn-primary ant-btn-lg">

我想检查按钮是否具有 disabled 属性,但是由于该属性中没有值,因此无法找到一种方法.

I want to check that the button has attribute disabled, however since the attribute has no value in it, I am not able to find to a way to do so.

例如,如果 disabled 属性具有类似这样的内容

For example, if the disabled attribute had something like this

<button type = "submit" class="ant-btn ant-btn-primary ant-btn-lg" disabled = "disabled">

然后我可以做类似的事情

then I can do something like this

let button = await page.$('button');
let valueHandle = await input.getProperty('disabled');
assert.equal(await valueHandle.jsonValue(), 'disabled');

但是由于该属性没有值,在这种情况下如何进行?

but since there is no value for the attribute, how to proceed in this case?

推荐答案

下面是一个全面的解决方案,显示了如何使用以下方法解决您的问题:

Here's a comprehensive solution showing how to solve your problem using:

page.$() page.$$() page.$ eval()

page.$(), page.$$(), page.$eval(), page.$$eval(), page.$x(), and page.evaluate().

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

// Using page.$$()
const is_disabled = (await page.$$('button[disabled]')).length !== 0;

// Using page.$eval()
const is_disabled = await page.$eval('button[disabled]', button => button !== null).catch(error => error.toString() !== 'Error: Error: failed to find element matching selector "button[disabled]"');

// Using page.$$eval()
const is_disabled = await page.$$eval('button[disabled]', buttons => buttons.length !== 0);

// Using page.$x()
const is_disabled = (await page.$x('//button[@disabled]')).length !== 0;

// Using page.evaluate()
const is_disabled = await page.evaluate(() => document.querySelector('button[disabled]') !== null);

这篇关于使用Puppeteer禁用检查元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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