Hash-based navigation with puppeteer

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 23:55:42

问题


I am trying to navigate to the url which contains # in the url which is returning Error: Navigation Timeout Exceeded: 30000ms exceeded

it('should go to new link', async function(){
    await page.goto('https://example.com/#/abc', {waitUntil: 'networkidle2'})
    await page.waitForNavigation()
    await page.waitFor(15000);
})

Original code expects a path to a tag having value as #/abc

    await page.waitForSelector('a tag selector')
    await page.click('a tag selector')
    await page.waitForNavigation()
    await page.waitFor(5000);
    await page.screenshot({ path: 'abc.png' })

Running the test file with command mocha --timeout 75000

package.json

"devDependencies": {
    "chai": "^4.1.2",
    "karma": "^2.0.3",
    "karma-chai": "^0.1.0",
    "karma-chrome-launcher": "^2.2.0",
    "karma-mocha": "^1.3.0",
    "karma-requirejs": "^1.1.0",
    "mocha": "^5.2.0"
},
"dependencies": {
  "puppeteer": "^0.12.0",
  "requirejs": "^2.3.5"

}

Updated the code to

await page.goto('https://example.com/#/abc', {waitUntil: 'load'})
await page.waitForSelector('selector on desired page');
await page.waitFor(5000);
await page.screenshot({ path: 'abc.png' })

and added headless: false, I can see it does go to the page wait for some amount of time but still throw the error Error: Navigation Timeout Exceeded: 30000ms exceeded


回答1:


goto automatically waits for navigation. Your waitForNavigation is expecting it to make another navigation request. You also don't need to wait for 25 seconds to make this right.

However for such ajax site, IMO, you should wait for some selector that loads only when the page is completely loaded.

You can also pass a timeout parameter to increase the timeout and see if that works.

The following is good enough.

it('should go to new link', async function(){
    await page.goto('https://example.com/#/abc', {waitUntil: 'networkidle2', timeout: 60000 })
    await page.waitFor('#SomeSelectorThatWeWaitFor')
})

The best way to debug this is to see the behaviour of target page carefully.



来源:https://stackoverflow.com/questions/50938926/hash-based-navigation-with-puppeteer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!