首页 > 解决方案 > 我无法使用 puppeteer 登录。在下面提供的网站中也找不到登录按钮的选择器

问题描述

Bellow是我正在尝试的代码,请帮助! 我面临两个问题,1. 浏览器在http://164.52.197.129/signin打开,但一段时间后又回到http://164.52.197.129 2. 我找不到登录按钮选择器。我选择的选择器不起作用,可能是因为它是嵌套的。

const puppeteer = require('puppeteer');
// const URL = 'http://164.52.197.129/signin';
const chromeOptions = {
  headless:false,
  defaultViewport: null};
(async function main() {
  const browser = await puppeteer.launch(chromeOptions);
  const page = await browser.newPage();
  //await page.setDefaultNavigationTimeout(0);
console.log("Opening page");
  await page.goto(('http://164.52.197.129/signin'), { waitUntil: 'networkidle2' , timeout: 60000 });
console.log("Page opened");
await page.waitForSelector('#email', {timeout: 60000});
console.log("Inputting username");
await page.type('#email', 'guest@gmail.com');
console.log("Username input completed");
await page.waitForSelector('#password', {timeout: 60000});
console.log("Inputting password");
await page.type('#password', 'sdah1234');
console.log("Password input completed");
await page.click('#app > div > main > div > div > div > form > div > div.v-card__text > div > div.text-xs-center.col > button');
await page.waitForNavigation({waitUntil: 'networkidle2'});
})()

标签: javascriptnode.jsweb-scrapingpuppeteer

解决方案


我会建议这样的算法:

  1. 打开页面。
  2. 等待重定向(出现轮播)。
  3. 要求再次签署表格(点击链接page.click()不起作用,所以我们正在使用page.evaluate())。
  4. 等待表格。
  5. 由于之前表单是自动完成page.type()的并且输入加倍,我们page.evaluate()再次使用。
  6. 单击并等待导航Promise.all()以避免竞争条件。
const puppeteer = require('puppeteer');

const chromeOptions = {
  headless:false,
  defaultViewport: null};

(async function main() {
  const browser = await puppeteer.launch(chromeOptions);
  const page = await browser.newPage();

  await page.goto(('http://164.52.197.129/signin'), { waitUntil: 'networkidle2' , timeout: 60000 });

  await page.waitForSelector('.carousel-3d-container');
  await page.waitForSelector('a[href="/signin"]');
  await page.evaluate(() => { document.querySelector('a[href="/signin"]').click(); });

  await page.waitForSelector('#email', {timeout: 60000});
  await page.waitForSelector('#password', {timeout: 60000});

  await page.evaluate(() => {
    document.querySelector('#email').value = 'guest@gmail.com';
    document.querySelector('#password').value = 'sdah1234';
  });

  await Promise.all([
    page.click('#app form button'),
    page.waitForNavigation({waitUntil: 'networkidle2'}),
  ]);

  console.log("Done");
})();


推荐阅读