在 facebook messenger 上使用 puppeteer 时没有选择器节点

unk*_*use 1 javascript automation facebook node.js puppeteer

我正在使用 Puppeteer 为我的家庭项目制作 Facebook Messenger api(有点)。

到目前为止,我可以成功使用 puppeteer 登录我的帐户。

当我想在登录后自动化时,真正的问题就开始了。

我无法点击任何元素。


例如

我想点击小“我”图标

在此处输入图片说明

然后我复制了选择器,如

在此处输入图片说明

我得到的CONSO以下错误文件

(node:4771) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): AssertionError [ERR_ASSERTION]: No node found for selector: #cch_feb64f1b00e628 > div._5742 > ul > li:nth-child(4) > a > div > svg
(node:4771) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Run Code Online (Sandbox Code Playgroud)

代码

const puppeteer = require('puppeteer');

(async function() {

    // This function is used to login into the account.
    async function login() {
        console.log('=====In Login=====');
        const email = '#email';
        const pass = '#pass';
        const submit = '#loginbutton';
        await page.waitFor(3000);
        await page.click(email);
        await page.waitFor(2000);
        await page.keyboard.type('not_a_real_email@mail.com');
        await page.waitFor(2000);
        await page.click(pass);
        await page.waitFor(2000);
        await page.keyboard.type('not_a_real_password');
        await page.waitFor(2000);
        await page.click(submit);
        return 0;
    }

    const browser = await puppeteer.launch({ headless: false });
    const page = await browser.newPage();


    page.on('load', () => console.log('=====Page loaded!====='));

    await page.goto('https://messenger.com');
    await login();
    await page.waitForNavigation();
    await page.waitFor(3000); // I'm waiting just for safe side to make sure the element is loaded.

    // This is the line where the 'i' is clicked.
    // This is the line error occurs.
    await page.click(
        '#cch_feb64f1b00e628 > div._5742 > ul > li:nth-child(4) > a > div > svg'
    );

})();
Run Code Online (Sandbox Code Playgroud)

所以,根据上面的错误,我知道选择器是错误的。那么什么是正确的选择器呢?不仅如此,当我在 emoji 元素等其他元素上尝试时,我也遇到了同样的错误。

Mic*_*ook 5

问题似乎是您使用的选择器是在运行时动态生成的,并且每次页面刷新都会更改。假设您只想单击 I(信息)按钮,以下使用 data-testid 的单击/选择器对我有用:

await page.click(
    '[data-testid="info_panel_button"]'
);
Run Code Online (Sandbox Code Playgroud)

如果您想测试顶部的其他图标,不幸的是,它们似乎没有相同的 data-testid,这意味着选择它们会更具挑战性。

完整示例

const puppeteer = require('puppeteer');

(async function() {

  // This function is used to login into the account.
  async function login() {
    console.log('=====In Login=====');
    const email = '#email';
    const pass = '#pass';
    const submit = '#loginbutton';
    await page.waitFor(3000);
    await page.click(email);
    await page.waitFor(2000);
    await page.keyboard.type('not_a_real_email@mail.com');
    await page.waitFor(2000);
    await page.click(pass);
    await page.waitFor(2000);
    await page.keyboard.type('not_a_real_password');
    await page.waitFor(2000);
    await page.click(submit);
    return 0;
  }

  const browser = await puppeteer.launch({ headless: false });
  const page = await browser.newPage();


  page.on('load', () => console.log('=====Page loaded!====='));

  await page.goto('https://messenger.com');
  await login();
  await page.waitForNavigation();
  await page.waitFor(3000); // I'm waiting just for safe side to make sure the element is loaded.

  // This is the line where the 'i' is clicked.
  // This is the line error occurs.
  await page.click(
      '[data-testid="info_panel_button"]'
  );})();
Run Code Online (Sandbox Code Playgroud)