The strongest automated testing framework Playwright (4) - context and window processing

browser.new_context() Creates a new browser context. It does not share cookies/cache with other browser contexts.

Contexts are isolated from each other and can be understood as lightweight browser instances

multiple browser windows

Creating a context is equivalent to opening a new browser window

The following code opens two browser windows:

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(headless=False, slow_mo=1000)
    context1 = browser.new_context()  # 创建上下文,浏览器实例1
    context2 = browser.new_context()  # 创建上下文,浏览器实例2
    page1 = context1.new_page()    # 打开标签页1
    page1.goto("https://www.baidu.com/")

    # 操作第二个浏览器窗口
    page2 = context2.new_page()  # 打开标签页1
    page2.goto("https://www.baidu.com/")

Creating a page is equivalent to opening a new tab

multi tab page

Each browser context can host multiple pages (tabs).

  • Each page acts like a focused active page.

    There is no need to bring the page to the front.

The following code opens two tab pages


from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(headless=False, slow_mo=1000)
    context = browser.new_context()  # 创建上下文,浏览器实例
    page = context.new_page()    # 打开标签页1
    page.goto("https://www.baidu.com/")
    page2 = context.new_page()  # 打开标签页2
    page2.goto("https://www.sina.com/")

    context.close()
    browser.close()

Browser context can also be used to simulate multi-page scenarios involving mobile devices, permissions, locales, and color schemes

Playwright can create multiple browser contexts in one scene. This is useful when you want to test multi-user functionality such as chat.

python+playwright learning-5.new_context context and new window operation

new tab page

The event page in the browser context can be used to get new pages created in the context. This can be used to handle new pages opened via target="_blank" links.

from playwright.sync_api import sync_playwright


with sync_playwright() as p:
    browser = p.chromium.launch(headless=False, slow_mo=1000)
    context = browser.new_context()  # 创建上下文,浏览器实例

    page = context.new_page()    # 打开标签页
    page.goto("https://www.baidu.com/")
    print(page.title())
    # Get page after a specific action (e.g. clicking a link)
    with context.expect_page() as new_page_info:
        page.click('text=新闻')  # Opens a new tab
    new_page = new_page_info.value

    new_page.wait_for_load_state()  # 等待页面加载到指定状态
    print(new_page.title())

Handle popups

If the page opens a popup (such as a page opened via a link), you can get a reference to it by listening to the event target="_blank" on the page. popup
This event is emitted in addition to the browserContext.on('page') event, but only for popups related to this page.

# Get popup after a specific action (e.g., click)
with page.expect_popup() as popup_info:
    page.get_by_text("open the popup").click()
popup = popup_info.value

popup.wait_for_load_state()
print(popup.title())

Guess you like

Origin blog.csdn.net/seanyang_/article/details/132240612