javascript - 我无法使用 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'});
})()
解决方案
我会建议这样的算法:
- 打开页面。
- 等待重定向(出现轮播)。
- 要求再次签署表格(点击链接
page.click()
不起作用,所以我们正在使用page.evaluate()
)。 - 等待表格。
- 由于之前表单是自动完成
page.type()
的并且输入加倍,我们page.evaluate()
再次使用。 - 单击并等待导航
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");
})();
推荐阅读
- c++ - c++代码中的奇怪赋值是什么?[&](){/*代码*/}();
- apache - mod_rewrite 子域(带路径)到 URL
- python - SQLAlchemy 意外警告
- wordpress - 将 wordpress 网站从 Salient 转换为 Elementor
- excel - 从工作簿上的说明表创建工作簿按钮
- mkdocs - mkdocs - 子页面的动态列表
- typescript - 如何将枚举限制为仅其成员
- python - 具有 datetime64 类型的 Pandas df,仅显示为年月日,例如 '2021-01-01'
- python - 在 GridSearchCV 的 roc_auc 分数中手动复制 Sklearn SVC
- stored-procedures - 循环通过服务器获取统计信息 (SSMS) 时找不到存储过程“sp_msforeachtable”