我尝试登录并使用puppeteer / node js登录后执行其他操作 它接受登录,并且在登录帐户后仍未执行任何操作
这是我的代码
const puppeteer = require('puppeteer');
async function log_in() {
const browser =await puppeteer.launch({
headless:false,
args: ['--Window-size=1929,1170','--Window-position=0,0']
});
const page = await browser.newPage();
await page.setViewport({'width': 1366, 'height': 768 });
await page.goto('url');
await page.click('.fancybox-item');
await delay(1000);
// fun for waiting
function delay(time) {
return new Promise(function(resolve) {
setTimeout(resolve, time)
});
}
const UserName= '070xxxxxx';
const Password= '222222';
page.click('.icon-Signin');
await delay(1000);
await page.type('#loginform-username', UserName);
await page.type('#loginform-password', Password);
//Go to the next page
await Promise.all([
page.waitForNavigation(), // The promise resolves after navigation has finished
page.click('.greenBtn'), // Clicking the link will indirectly cause a navigation
]);
await delay(5000);
await page.click('#header > div > div.userMenu.mt15.mb15.fLeft > ul > li:nth-child(5) > a');
await delay(5000);
await browser.close();
}log_in();
答案 0 :(得分:0)
您的代码中有几点可能会导致问题:
尝试调查登录如何发生。 page.waitForNavigation()
方法等待使用History API进行表单提交或URL更改。如果没有实际执行导航,则可以使用page.waitForSelector
或page.waitForXPath
方法等待任何特定元素的出现。否则,请尝试使用page.waitForNavigation({ waitUntil: 'networkidle0' })
使用超时来等待异步任务的结果不是一个好习惯。您可能至少会面对网络速度变慢的情况。因此,脚本可能会在完成预期操作之前从await delay(...);
中退出。我建议您在单击page.waitFor*
之后,等待.fancybox-item
的出现,以显示其他任何登录弹出窗口。
您实际上并不在等待page.click('.icon-Signin');
的结果。在某些情况下,点击事件处理程序可能会花费更多时间。不要忘了await
。
希望有帮助!
答案 1 :(得分:0)
page.waitFor
和page.waitForSelector
或page.waitForNavigation
就足够了。page.waifForNavigation
。这意味着该功能开始监听页面事件,并在导航开始时解决诺言。通常会发生这种错误,因为人们在导航事件本身发生之后放置了page.waitForNavigation
。因此,在这里,代码已经为您编辑。告诉我这是否完美。
const puppeteer = require('puppeteer')
const UserName= '070xxxxxx'
const Password= '222222'
const log_in = async () => {
// fun for waiting
function delay(time) {
return new Promise(function(resolve) {
setTimeout(resolve, time)
})
}
const browser = await puppeteer.launch({
headless:false,
args: ['--Window-size=1929,1170','--Window-position=0,0']
})
const page = (await browser.pages())[0]
page.setViewport({'width': 1366, 'height': 768 })
page.setDefaultNavigationTimeout(0)
await page.goto('url', {waitUntil: 'domcontentloaded'})
// Is line below start navigation event? If so, use the commented lines below it
await page.click('.fancybox-item')
// const clickFancy = await Promise.all([
// page.waitForNavigation({waitUntil: 'domcontentloaded'}), // <= change it to load or networkidle0 if you like
// page.click('.fancybox-item')
// ])
// await delay(1000) // => DO NOT USE THIS IF YOU'RE NOT SURELY WAITING FOR ANY TIMER
// Is line below fire a navigation event? If so, use the commented lines below it
page.click('.icon-Signin')
// const clickLogin = await Promise.all([
// page.waitForNavigation({waitUntil: 'domcontentloaded'}), // <= change it to load or networkidle0 if you like
// page.click('.icon-Signin')
// ])
// await delay(1000) // => DO NOT USE THIS IF YOU'RE NOT SURELY WAITING FOR ANY TIMER
await page.type('#loginform-username', UserName)
await page.type('#loginform-password', Password)
//Go to the next page
const nextPage = await Promise.all([
page.waitForNavigation('domcontentloaded'), // The promise resolves after navigation has finished
page.click('.greenBtn') // Clicking the link will indirectly cause a navigation
])
// await delay(5000) => DO NOT USE THIS IF YOU'RE NOT SURELY WAITING FOR ANY TIMER
// Is line below fire a navigation event? If so, use the commented lines below it
await page.click('#header > div > div.userMenu.mt15.mb15.fLeft > ul > li:nth-child(5) > a')
// const clickFancy = await Promise.all([
// page.waitForNavigation({waitUntil: 'domcontentloaded'}), // <= change it to load or networkidle0 if you like
// await page.click('#header > div > div.userMenu.mt15.mb15.fLeft > ul > li:nth-child(5) > a')
// ])
// await delay(5000) => DO NOT USE THIS IF YOU'RE NOT SURELY WAITING FOR ANY TIMER
await browser.close()
}
log_in()