如何将值传递给正在等待 API 值的 puppeteer 程序

发布于 2025-01-15 14:30:38 字数 1331 浏览 5 评论 0原文

您好,我正在运行一个木偶脚本,它将预先填充表单值并等待来自命令行的值。一旦收到该值,它将在输入类型中输入该值并提交表单。

我可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幽梦紫曦~ 2025-01-22 14:30:38

我遇到了类似的问题。

这对我有用:

...
const input = await page.$('#id');
await input.evaluate(
(inp, { username })=>{
    inp.value = username;
},
{ username: username } // pass params from async function here
)

I faced on similar problem.

This works for me:

...
const input = await page.$('#id');
await input.evaluate(
(inp, { username })=>{
    inp.value = username;
},
{ username: username } // pass params from async function here
)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文