gpt4 book ai didi

node.js - 是否可以检测元素是否存在,然后提示用户输入?

转载 作者:太空宇宙 更新时间:2023-11-03 22:25:43 24 4
gpt4 key购买 nike

所以我有一个用例场景,我想检查是否存在某些东西,即安全复选框。如果是,则提示用户输入。我已经有另一个包含提示的包,但不确定如何实现将其与此结合起来。

var result = await new Promise( function (resolve, reject) {
prompt.get(schema, function (err, result) {
console.log('Command-line input received:');
console.log(' password: ' + result.password);
resolve(result);
reject("error with prompt")
})

});
console.log(result);
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.goto(url);

try {
await page.waitFor("#security", { timeout: 3000 });
// do something if there is a security box
}
catch (e) {
console.error('no security');
}

编辑

只需使用 try catch 和 waitFor 即可进行检查

try {
await page.waitFor("#login-form-os-captcha", { timeout: 1000 });
console.log("there is captcha")
}
catch (e) {
console.error('no captcha');
// process.exit(1);
}

最佳答案

编辑 promise :

//...


// create a promise with the search in the page
searchElement(selector) => new Promise( function (resolve, reject){
try{
var elemAwait = await page.$eval(selector, el => el);
resolve(selector,elemAwait);
}catch{
reject(selector,"Element doesn't exist");
}
});

// now, search element in the headless page
searchElement('#security')
.then(function(selector,el){
// element is here
prompt.get(schema, function (err, result) {

console.log(selector+' is here with value:' + el.value);

});
})
.catch(function(selector,msg){
// element isn't here
prompt.get(schema, function (err, result) {

console.log(selector+" - "+msg);


});
});

相对于:

https://developers.google.com/web/updates/2017/04/headless-chrome#cri

https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pageselector

https://github.com/flatiron/prompt

关于node.js - 是否可以检测元素是否存在,然后提示用户输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46879924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com