问题描述
这是我使用Facebook登录到我正在自动化的网站的代码:
This is my code to login using facebook to a website I'm automating:
const loginButton = await page.waitForXPath(
"//button[contains(@name, 'login')]"
);
const email = await page.waitForSelector("#email");
const pass = await page.waitForSelector("#pass");
await page.evaluate((text) => {
email.value = text;
}, "my email");
await page.evaluate((text) => {
pass.value = text;
}, "my password");
await loginButton.click();
通常它可以正常工作,但是每4到5次,单击loginButton后出现以下错误:
Usually it works well, but once every 4 or 5 times, I get the following error after clicking the loginButton:
需要Cookie.浏览器未启用Cookie.请在浏览器首选项中启用Cookie才能继续."
"Cookies required. Cookies are not enabled on your browser. Please enable cookies in your browser preferences to continue."
我从Chromium更改为Chrome,以查看这是否可以解决问题,但是没有用.
I changed from Chromium to Chrome to see if this would solve the issue, but it didn't work.
我还检查了cookie设置并启用了它们.
I also checked the cookies settings and they are enabled.
推荐答案
问题是输入内容被迅速填充,facebook怀疑我不是一个真实的人.我通过在登录步骤之间引入一些延迟来解决了该问题:
The problem was that the inputs were being filled to quickly and facebook was suspecting that I wasn't a real person. I solved it by introducing some delay between the login steps:
const loginButton = await page.waitForXPath(
"//button[contains(@name, 'login')]"
);
const email = await page.waitForSelector("#email");
const pass = await page.waitForSelector("#pass");
await page.waitFor(1000);
await page.evaluate((text) => {
email.value = text;
}, "[email protected]");
await page.waitFor(1000);
await page.evaluate((text) => {
pass.value = text;
}, "789654123");
await page.waitFor(1000);
await loginButton.click();
这篇关于无法使用Puppeteer通过Facebook登录-错误:需要Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!