在 puppeteer 中的选项卡之间切换 [英] Switching between tabs in puppeteer
本文介绍了在 puppeteer 中的选项卡之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用 puppeteer 在当前打开的标签页和父标签页之间切换?
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.
推荐答案
您可以使用 browser.pages()
方法,它返回一个承诺,该承诺解析为所有打开页面的数组.
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];
此外,您可以使用 event popup
,当页面打开新标签或窗口时发出.
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')),
]);
这篇关于在 puppeteer 中的选项卡之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文