[英]No node for selector while using puppeteer on facebook messenger
我正在使用 Puppeteer 為我的家庭項目制作 Facebook Messenger api(有點)。
到目前為止,我可以成功使用 puppeteer 登錄我的帳戶。
當我想在登錄后自動化時,真正的問題就開始了。
我無法點擊任何元素。
例如:
我想點擊小“i”圖標:
然后我復制了選擇器,如:
我在控制台中收到以下錯誤:
(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.
代碼:
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'
);
})();
所以,根據上面的錯誤,我知道選擇器是錯誤的。 那么什么是正確的選擇器呢? 不僅如此,當我在 emoji 元素等其他元素上嘗試時,我也遇到了同樣的錯誤。
問題似乎是您使用的選擇器是在運行時動態生成的,並且每次頁面刷新都會更改。 假設您只想單擊 I(信息)按鈕,以下使用 data-testid 的單擊/選擇器對我有用:
await page.click(
'[data-testid="info_panel_button"]'
);
如果您想測試頂部的其他圖標,不幸的是,它們似乎沒有相同的 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"]'
);})();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.