带有提示的 Puppeteer,忽略其余功能
Puppeteer with Prompt, ignoring rest of function
我已经设法让我的代码等待提示,但由于某种原因它在收到提示后没有继续。输出为:
prompt: password:
Command-line input received:
password: password
代码如下:
const puppeteer = require('puppeteer');
const prompt = require('prompt');
const url = 'some url';
var schema = {
properties: {
password: {
hidden: true
}
}
};
prompt.start();
(async () => {
var result = await new Promise( function (resolve, reject) {
return prompt.get(schema, function (err, result) {
console.log('Command-line input received:');
console.log(' password: ' + result.password);
return result;
})
});
console.log(result);
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.goto(url);
/* truncated */
})();
你必须兑现承诺。我重新编写了一些代码以在实践中展示这个概念。
const puppeteer = require('puppeteer');
const prompt = require('prompt');
var schema = {
properties: {
url: {
hidden: false
}
}
};
prompt.start();
(async() => {
var result = await new Promise(function (resolve, reject) {
prompt.get(schema, function (err, result) {
resolve(result);
});
});
const browser = await puppeteer.launch({
headless: true
});
const page = await browser.newPage();
await page.goto(result.url);
const title = await page.title();
console.log(`${result.url} has title: ${title}`);
await page.close();
await browser.close();
/* truncated */
})();
和运行它将产生以下结果。
▶ node prompt.js
prompt: url: http://example.com
http://example.com has title: Example Domain
▶ node prompt.js
prompt: url: http://google.com
http://google.com has title: Google
祝你好运。
我已经设法让我的代码等待提示,但由于某种原因它在收到提示后没有继续。输出为:
prompt: password:
Command-line input received:
password: password
代码如下:
const puppeteer = require('puppeteer');
const prompt = require('prompt');
const url = 'some url';
var schema = {
properties: {
password: {
hidden: true
}
}
};
prompt.start();
(async () => {
var result = await new Promise( function (resolve, reject) {
return prompt.get(schema, function (err, result) {
console.log('Command-line input received:');
console.log(' password: ' + result.password);
return result;
})
});
console.log(result);
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.goto(url);
/* truncated */
})();
你必须兑现承诺。我重新编写了一些代码以在实践中展示这个概念。
const puppeteer = require('puppeteer');
const prompt = require('prompt');
var schema = {
properties: {
url: {
hidden: false
}
}
};
prompt.start();
(async() => {
var result = await new Promise(function (resolve, reject) {
prompt.get(schema, function (err, result) {
resolve(result);
});
});
const browser = await puppeteer.launch({
headless: true
});
const page = await browser.newPage();
await page.goto(result.url);
const title = await page.title();
console.log(`${result.url} has title: ${title}`);
await page.close();
await browser.close();
/* truncated */
})();
和运行它将产生以下结果。
▶ node prompt.js
prompt: url: http://example.com
http://example.com has title: Example Domain
▶ node prompt.js
prompt: url: http://google.com
http://google.com has title: Google
祝你好运。