How can I open a new browser tab from within a router handler?

Hi there, I'm currently running a PlaywrightCrawler with crawlee and I would like to open a new tab from within a route handler, so that I can run the following:
router.addHandler('*/ergebnisse.xhtml', async ({ request, page, log, session }) => {
// create a new tab & start navigating it concurrently
const browserContext = await page.browserContext();
const newTab = await browserContext.newTab.goTo("*/documents-dk.xhtml");
const [_, data] = await Promise.all([
// 1. initiate navigation on the new tab
navigateThroughPage(newTab);
// 2. Now, do stuff on current `page`
downloadSomething(page);
])
// finally, do some stuff on the new Tab
const data2 = await downloadSomething(newTab);
})
router.addHandler('*/ergebnisse.xhtml', async ({ request, page, log, session }) => {
// create a new tab & start navigating it concurrently
const browserContext = await page.browserContext();
const newTab = await browserContext.newTab.goTo("*/documents-dk.xhtml");
const [_, data] = await Promise.all([
// 1. initiate navigation on the new tab
navigateThroughPage(newTab);
// 2. Now, do stuff on current `page`
downloadSomething(page);
])
// finally, do some stuff on the new Tab
const data2 = await downloadSomething(newTab);
})
Would that actually work? NB: The website supports only downloading one single request at a time via cookies. Since the navigation on the new tab takes some time though, I'd like to run that concurrently in two tabs while the first request downloads. Then, after that's done, i'd like to initiate the second download on the already pre-navigated page. LMK if the context here is not clear.
1 Reply
Pepa J
Pepa J2y ago
Not sure if I understand, but you can use regular Playwright calls inside the router. Does https://stackoverflow.com/a/64279379 answers your question. Beware, you probably need to wait for the newly open tab to close and finish, otherwise procession your initial route tab may finish sooner before the new opened one is finished, At that point you get and exception.
Stack Overflow
How to open the new tab using Playwright (ex. click the button to o...
I am looking for a simpler solution to a current situation. For example, you open the google (any another website) and you want BY CLICK on the button (ex. Gmail) - open this page in the new tab us...

Did you find this page helpful?