伪造者/登录后不接受任何操作

时间:2019-11-01 16:48:26

标签: node.js puppeteer apify

我尝试登录并使用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();

  • 如果任何人都可以查看该代码并知道该问题以帮助我修复它 注意:我在注册时遇到了同样的问题,它成功注册,并且在访问该网站后从未执行任何操作

2 个答案:

答案 0 :(得分:0)

您的代码中有几点可能会导致问题:

  1. 尝试调查登录如何发生。 page.waitForNavigation()方法等待使用History API进行表单提交或URL更改。如果没有实际执行导航,则可以使用page.waitForSelectorpage.waitForXPath方法等待任何特定元素的出现。否则,请尝试使用page.waitForNavigation({ waitUntil: 'networkidle0' })

  2. 使用超时来等待异步任务的结果不是一个好习惯。您可能至少会面对网络速度变慢的情况。因此,脚本可能会在完成预期操作之前从await delay(...);中退出。我建议您在单击page.waitFor*之后,等待.fancybox-item的出现,以显示其他任何登录弹出窗口。

  3. 您实际上并不在等待page.click('.icon-Signin');的结果。在某些情况下,点击事件处理程序可能会花费更多时间。不要忘了await

希望有帮助!

答案 1 :(得分:0)

  1. 如果您不等待真正的计时器,请不要依赖诸如delay或setTimeout函数。如果您只需要一些时间来等待导航或任何UI更改,那么page.waitForpage.waitForSelectorpage.waitForNavigation就足够了。
  2. 在发生导航事件之前使用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()