Puppeteer: How to handle multiple tabs?

后端 未结 8 819
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-13 09:09

Scenario: Web form for developer app registration with two part workflow.

Page 1: Fill out developer app details and click on button to create Application ID, which

8条回答
  •  一整个雨季
    2020-12-13 09:29

    A new patch has been committed two days ago and now you can use browser.pages() to access all Pages in current browser. Works fine, tried myself yesterday :)

    Edit:

    An example how to get a JSON value of a new page opened as 'target: _blank' link.

    const page = await browser.newPage();
    await page.goto(url, {waitUntil: 'load'});
    
    // click on a 'target:_blank' link
    await page.click(someATag);
    
    // get all the currently open pages as an array
    let pages = await browser.pages();
    
    // get the last element of the array (third in my case) and do some 
    // hucus-pocus to get it as JSON...
    const aHandle = await pages[3].evaluateHandle(() => document.body);
    
    const resultHandle = await pages[3].evaluateHandle(body => 
      body.innerHTML, aHandle);
    
    // get the JSON value of the page.
    let jsonValue = await resultHandle.jsonValue();
    
    // ...do something with JSON
    

提交回复
热议问题