I'm new to Playwright so expect this is my misunderstanding, but any help gratefully received.
When using Python Playwright (1.31.1) to click a link on a page which opens a new page in a new tab, I'd assumed the list length returned by BrowserContext.pages would increase by 1. This doesn't appear to be the case, at least not immediately following the click.
Hopefully illustrated in the simplified snippet below. I was expecting the second print to report 2 pages, but shows 1. If I then open a third page using new_page() the number of reported pages jumps from 1 to 3 (correct value). I've tried playing with Page and Context events but can't catch the second page...
Is this expected Playwright behaviour?
# Using palywright 1.31.1
from playwright.sync_api import sync_playwright
from time import sleep
with sync_playwright() as pw:
browser = pw.chromium.launch(
executable_path = 'C:/Program Files/Google/Chrome/Application/chrome.exe',
headless=False)
ctx = browser.new_context()
page = ctx.new_page()
print('1. Number of pages', len(ctx.pages))
page.goto("https://www.dofactory.com/html/target/blank")
# Click lick which will open new page.
g = page.locator('.mt-20').get_by_text('google')
g.click()
sleep(5)
print('2. Number of pages (should be 2)', len(ctx.pages))
# Open then close a new page.
ctx.new_page()
sleep(5)
print('3. Number of pages (should be 3)', len(ctx.pages))
ctx.pages[-1].close()
sleep(5)
print('4. Number of pages (should be 2)', len(ctx.pages))
Output:
- Number of pages 1
- Number of pages (should be 2) 1
- Number of pages (should be 3) 3
- Number of pages (should be 2) 2