问题
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