Puppeteer:如何提交表格?

时间:2017-08-20 03:26:43

标签: javascript node.js google-chrome-headless puppeteer

使用puppeteer,您如何以编程方式提交表单?到目前为止,如果表单实际包含提交输入,我已经能够使用page.click('.input[type="submit"]')执行此操作。但是对于不包含提交输入的表单,关注表单文本输入元素并使用page.press('Enter')似乎并不会导致表单提交:

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();

})();

2 个答案:

答案 0 :(得分:22)

试试这个

SELECT ... FROM b WHERE a_id IN (the list you just got)
SELECT ... FROM c WHERE a_id IN (the list you just got)

对于v0.11.0和laters:

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

答案 1 :(得分:15)

如果您尝试填写并提交登录表单,则可以使用以下内容:

await page.goto('https://www.example.com/login');

await page.type('#username', 'username');
await page.type('#password', 'password');

await page.click('#submit');

await page.waitForNavigation();

console.log('New Page URL:', page.url());