up:如何提交表格? [英] Puppeteer: How to submit a form?

查看:47
本文介绍了up:如何提交表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用木偶,您如何以编程方式提交表单?到目前为止,如果表单实际上包括一个提交输入,我已经可以使用 page.click('.input [type ="submit"]')来做到这一点.但是对于不包含提交输入的表单,关注表单文本输入元素并使用 page.press('Enter')似乎并没有真正导致表单提交:

Using puppeteer, how could you programmatically submit a form? So far I've been able to do this using page.click('.input[type="submit"]') if the form actually includes a submit input. But for forms that don't include a submit input, focusing on the form text input element and using page.press('Enter') doesn't seem to actually cause the form to submit:

const puppeteer = require('puppeteer');

(async() => {

    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto('https://stackoverflow.com/', {waitUntil: 'load'});
    console.log(page.url());

    // Type our query into the search bar
    await page.focus('.js-search-field');
    await page.type('puppeteer');

    // Submit form
    await page.press('Enter');

    // Wait for search results page to load
    await page.waitForNavigation({waitUntil: 'load'});


    console.log('FOUND!', page.url());

    // Extract the results from the page
    const links = await page.evaluate(() => {
      const anchors = Array.from(document.querySelectorAll('.result-link a'));
      return anchors.map(anchor => anchor.textContent);
    });
    console.log(links.join('\n'));
    browser.close();

})();

推荐答案

尝试一下

const form = await page.$('form-selector');
await form.evaluate(form => form.submit());


对于v0.11.0及更高版本:


For v0.11.0 and laters:

await page.$eval('form-selector', form => form.submit());

这篇关于up:如何提交表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆