0

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:

  1. Number of pages 1
  2. Number of pages (should be 2) 1
  3. Number of pages (should be 3) 3
  4. Number of pages (should be 2) 2
ggorlen
  • 44,755
  • 7
  • 76
  • 106
COB
  • 1
  • Sounds like the increase for 1 to 2 is due to a racing issue like you mentioned, where you getting the length happens before the click causes a new page to be added. Waiting didn’t help? What exactly did you try when waiting for the new page event? – David R Apr 11 '23 at 23:02
  • As for the closed part, the page instance of the context still exists even if closed, so you would have to use the is_closed() method to filter or determine counts. – David R Apr 11 '23 at 23:04
  • Thanks David. No, waiting didn't help. Having re-read the docs (https://playwright.dev/python/docs/library#timesleep-leads-to-outdated-state) I changed the time.sleep() to page.wait_for_timeout(), but that didn't help either. – COB Apr 13 '23 at 09:16
  • With regards to events, I registered 'backgroundpage' & 'page' for context, and 'popup' to page - using the ctx/page.on(, ) syntax - but none of the event handlers fire. At the moment, I'm running with the hack to open/close a page to trigger page count update. Seems to work though is horrible! (Thanks for the is_closed() tip BTW). – COB Apr 13 '23 at 09:21
  • That’s really odd that none of that worked That hack makes sense to work as it would actually be waiting for the new page to be created/added… and no problem on the tip! For the events, I would expect the ‘page’ event on the context to be the desired one. Just to clarify/be sure, were you calling ‘on’/setting up the handler before or after the click that should cause the new page to happen? – David R Apr 13 '23 at 18:20
  • With a little more playing around, I've managed to catch the new page opening with an event: page.('popup', ). No events on the context. On my system it takes around a second from the click to the event handler firing. – COB Apr 15 '23 at 10:13

0 Answers0