Switching between tabs in puppeteer

落爺英雄遲暮 提交于 2019-12-11 16:55:35

问题


How to switch between current opened tab to the parent tab using puppeteer?

I have a home page and when i click on the link in home page it opens a new tab. After that page is loading i need to switch to home page.


回答1:


You can use the browser.pages() method, it returns a promise which resolves to an array of all open pages.

const firstTab = (await browser.pages())[0];
const secondTab = (await browser.pages())[1];

Also, you can use event popup, emitted when the page opens a new tab or window.

const [popup] = await Promise.all([
  new Promise(resolve => page.once('popup', resolve)),
  page.click('a[target=_blank]'),
]);

const [popup] = await Promise.all([
 new Promise(resolve => page.once('popup', resolve)),
 page.evaluate(() => window.open('https://example.com')),
]);

Read more about events here.



来源:https://stackoverflow.com/questions/56963603/switching-between-tabs-in-puppeteer

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