首页 > 解决方案 > Puppeteer 输入值以输入表单

问题描述

如果表单字段是这样的,我该如何放置数据。

<input type="text" placeholder="Email Address" data-id="emailAddress" autocomplete="email" class="AuthenticationModal__AuthenticationTextInput-li0874-28 kuwMeF WebsiteTextInput-sc-1k4jzwj-3 kZlRsK" value="">
<input type="password" placeholder="Password" data-id="password" autocomplete="current-password" class="AuthenticationModal__AuthenticationTextInput-li0874-28 kuwMeF WebsiteTextInput-sc-1k4jzwj-3 kZlRsK" value="">

尝试使用它但没有用。

await page.type('[data-id="emailAddress"]', 'test_account@gmail.com')
await page.type('[data-id="password"]', '12345678')
await page.screenshot({ fullPage: true, path: 'website.png' })
await browser.close();

test_account@gmail.com 不显示在屏幕截图中。

提前致谢。

注意:我只是一个新手

标签: puppeteer

解决方案


告诉我这段代码是否完美运行。

const emailInput = '[placeholder="Email Address"][autocomplete="email"]'
const emailWords = 'test_account@gmail.com'
await page.waitForSelector( emailInput, { timeout: 0 })
await page.focus(emailInput)
await page.keyboard.type(emailWords)

const passwordInput = 'input[placeholder="Password"][autocomplete="current-password"]'
const passwordWords = '12345678'
await page.waitForSelector( passwordInput, { timeout: 0 })
await page.focus(passwordInput)
await page.keyboard.type(passwordWords)

await page.screenshot({ fullPage: true, path: 'website.png' })
await browser.close()

导航发生后,您当然不能 waitForNavigation 。您可以在单击表单中的提交之前使用 waitForNavigation。不要在这里使用任何 await,因为它已经是一个承诺。


Promise.all([
    page.waitForNavigation({ waitUntil: 'networkidle0', timeout: 1000000 })
    page.click('input[type="submit"]')
])

类似的东西。告诉我这是否有效。并且不要忘记选择此答案作为正确答案,它非常有效。


推荐阅读