如何将值传递给正在等待 API 值的 puppeteer 程序
您好,我正在运行一个木偶脚本,它将预先填充表单值并等待来自命令行的值。一旦收到该值,它将在输入类型中输入该值并提交表单。
我可以使用 readline() 来做到这一点。我面临的问题是我的脚本将等待 API 提供的值。
我需要知道如何从 API 接收值,以便我的脚本将使用该值来填写表单并继续。
const puppeteer = require('puppeteer');
const readline = require('readline');
async function readLine() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise(resolve => {
rl.question('Enter username: ', (answer) => {
rl.close();
resolve(answer)
});
});
(async () => {
const browser = await puppeteer.launch({
headless: false,
devtools: false,
ignoreHTTPSErrors: true,
args: [
'--start-fullscreen',
'--window-size=1920,1040',
'--no-sandbox'
]
});
const page = await browser.newPage();
// Set windows height en width
await page.setViewport({
width: 1600,
height: 900
});
await page.goto("www.domain.com/admin");
const userName = await readLine();
await page.focus('#username');
await page.keyboard.type(userName, {delay: 100});
})
这是我的代码。 我的脚本将等待该值。我需要将值传递给我的脚本。我将从另一个申请中收到该信息。我需要知道如何使用 API 将值传递给我的脚本
Hi I am running a puppeteer script which will pre fill the form values and will wait for the value from the command line. Once it receives the value it will enter the value in an input type and submit the form.
I am able to do it using readline(). The problem I'm facing is my script will be waiting for the value which will be provided by an API.
I need to know how to receive value from the API so that my script will use the value to fill the form and continue.
const puppeteer = require('puppeteer');
const readline = require('readline');
async function readLine() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise(resolve => {
rl.question('Enter username: ', (answer) => {
rl.close();
resolve(answer)
});
});
(async () => {
const browser = await puppeteer.launch({
headless: false,
devtools: false,
ignoreHTTPSErrors: true,
args: [
'--start-fullscreen',
'--window-size=1920,1040',
'--no-sandbox'
]
});
const page = await browser.newPage();
// Set windows height en width
await page.setViewport({
width: 1600,
height: 900
});
await page.goto("www.domain.com/admin");
const userName = await readLine();
await page.focus('#username');
await page.keyboard.type(userName, {delay: 100});
})
This is my code.
My script will be waiting for the value. I need to pass the value to my script. which I will receive from another application. I need to know how to pass the value to my script using an API
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了类似的问题。
这对我有用:
I faced on similar problem.
This works for me: