2

I am using peppeteer to retrieve website data and want to open another new tab (second tab) to retrieve more detailed data. The new tab will be closed after the specified data is retrieved and switch back to the first tab.

I had tried to open another new tab successfully and retrieve data but cannot switch back to the first tab.


    async def details(url, browser):
        detailPage = await browser.newPage()
        await detailPage.goto(url)
        ################
        #retrieve data
        ################
        detailPage.close()

    async def main(websiteURL):
        browser = await launch(headless=False)
        page = await browser.newPage()
        await page.setJavaScriptEnabled(enabled=True)
        await page.goto(websiteURL)
        ################
        #retrieve data and the URL
        ################
        await detail(url,browser)


    asyncio.get_event_loop().run_until_complete(main(websiteURL))

I expect the second tab will be closed and switch back to the first tab, but the browser is closed instead of the second tab.

Kester
  • 283
  • 2
  • 5
  • 10

1 Answers1

0

You can list the tabs in the browser with:

tabs = await browser.pages()

Switch between tabs by taking the right element:

page_tab1 = tabs[0]
page_tab2 = tabs[1]
page_tab3 = tabs[2]
...
erdogant
  • 1,544
  • 14
  • 23