Rayrun
← Back to Discord Forum

Hi everyone. How to let the different context open in different Tab but in the same browser?

hunterLeungposted in #help-playwright
Open in Discord
hunterLeung

Hi everyone. I want all the pages with different contexts open in the same browser . How should I fix my code ? Thanks for your help .

from playwright.async_api import BrowserContext import asyncio from playwright.async_api import async_playwright import time

async def main(queryList : list ): try: async with async_playwright() as p: ff = p.firefox browser = await ff.launch(headless=False) tastList = [run(await browser.new_context(), queryList[i]) for i in range(len(queryList))] results = await asyncio.gather(*tastList)

#print(results)
    return extract_data(await loc1.inner_text())

except Exception as e:
    print(e)
    return None

async def run(context : BrowserContext , query:str): global loc1 try: page = await context.new_page() await page.goto("https://xxxxxxx.com/checker") await page.get_by_placeholder("Enter query").click() await page.get_by_placeholder("Enter query").fill("some word") loc1 = page.get_by_role("dialog") #print(await loc1.inner_text()) return extract_data(await loc1.inner_text()) except Exception as e: print(e) return None

This thread is trying to answer question "How to let the different context open in different Tab but in the same browser?"

2 replies
hunterLeung

Hi everyone. How to make the different context open in a same browser window ?

hunterLeung

Hi everyone. How to let the different context open in different Tab but in the same browser?

Answer

Opening Multiple Pages in the Same Browser Window

First, you'll need to create a new browser context. This creates a separate environment for your pages to run in.

const { chromium } = require('@playwright/test');

(async () => {
  const browser = await chromium.launch();
  const context = await browser.newContext();
})();

Next, create multiple pages within the same context. Each page represents a separate tab or popup window within the browser.

const pageOne = await context.newPage();
const pageTwo = await context.newPage();

You can navigate to different URLs and interact with each page individually.

await pageOne.goto('https://ray.run');
await pageTwo.goto('https://ray.run/blog');

To handle new pages that are opened by links with target="_blank", use the waitForEvent() method on your context object.

const newPagePromise = context.waitForEvent('page');
await pageOne.click('a[target="_blank"]');
const newPage = await newPagePromise;

Ensure that all pages are loaded before performing any actions. You can use methods like waitForLoadState() or check for specific elements on each page.

await Promise.all([
  pageOne.waitForLoadState(),
]);

By following these steps, you can have multiple pages with different contexts open in the same browser window and perform actions on them individually. Remember to handle any errors or exceptions that may occur during the execution of your code.

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].