Skip to content

[BUG]when using connect_over_cdp, the browser_context.pages returned pages missing child_frames #17656

Closed

Description

@zhanbinyy

Context:

  • Playwright Version: 1.25.2
  • Operating System: Windows
  • Python version: 3.8.2
  • Browser: Chromium

Code Snippet

    async def connect_to_browser(self):
        if not self._playwright:
            self._playwright = await async_playwright().start()
        self._browser = await self._playwright.chromium.connect_over_cdp(f"http://localhost:9222")
        self._browser_context = self._browser.contexts[0]
        pages = self._browser_context.pages 

Describe the bug
when using connect_over_cdp, the browser_context.pages returned pages missing child_frames
image

but when i using browser_context.new_page() and page.goto(url), (same url) the returned page contain child_frames
image

Activity

  1. mxschmitt commented on Sep 27, 2022

    @mxschmitt
    Contributor

    I tried to reproduce using the following script, but it was working for me. Could you share more information about the site you are testing against?

    from playwright.sync_api import sync_playwright
    
    with sync_playwright() as p:
        browser = p.chromium.connect_over_cdp("http://127.0.0.1:9222")
        print(len(browser.contexts[0].pages[0].main_frame.child_frames))
        browser.close()

    Do you navigate using Chromium directly or do you connect with some other CDP client?

  2. zhanbinyy commented on Sep 28, 2022

    @zhanbinyy
    Author

    I tried to reproduce using the following script, but it was working for me. Could you share more information about the site you are testing against?

    from playwright.sync_api import sync_playwright
    
    with sync_playwright() as p:
        browser = p.chromium.connect_over_cdp("http://127.0.0.1:9222")
        print(len(browser.contexts[0].pages[0].main_frame.child_frames))
        browser.close()

    Do you navigate using Chromium directly or do you connect with some other CDP client?

    my testing site is: https://web.txcs.tmall.com/login

  3. mxschmitt commented on Sep 28, 2022

    @mxschmitt
    Contributor

    I can repro upstream the differences:

    import { chromium } from 'playwright';
    
    (async () => {
      // Setup
      const browser = await chromium.launch();
      const page = await browser.newPage();
      await page.goto('https://web.txcs.tmall.com/login');
      // const browser = await chromium.connectOverCDP('http://localhost:9222');
      // const page = browser.contexts()[0].pages()[0]
      console.log(page.mainFrame().childFrames().length);
      await browser.close();
    })();

    With launch() its 3 and with cdp its 0 when navigated manually.

  4. yury-s commented on Oct 4, 2022

    @yury-s
    Member

    The sites loads the frames after the navigation, sometimes I see 2 frames, sometimes 3 and sometimes 4. If I add timeout after goto it normally stabilizes at 4 frames regardless of whether it's local browser or CDP connection:

    import { chromium } from 'playwright';
    
    (async () => {
      // Setup
      // const browser = await chromium.launch({headless: false});
      // const page = await browser.newPage();
      const browser = await chromium.connectOverCDP('http://127.0.0.1:9222');
      const page = browser.contexts()[0].pages()[0]
      await page.goto('https://web.txcs.tmall.com/login');
      await new Promise(f => setTimeout(f, 3000));
      console.log(page.mainFrame().childFrames().length);
      await browser.close();
    })();

    This is working as intended, you should way for a certain condition when the frames stop changing in the page ('load' event doesn't meet this criteria). Feel free to open another issue and link it to this one if you think there is a bug in Playwright, we'll need a reproducible example to act on that.

  5. mxschmitt commented on Oct 4, 2022

    @mxschmitt
    Contributor

    I can still repro with the following:

    import { chromium } from 'playwright';
    
    (async () => {
      const disposables = [];
    
      {
        const browserServer = await chromium.launch({
          args: ['--remote-debugging-port=9999'],
          headless: false,
        });
        disposables.push(browserServer);
        const page = await browserServer.newPage();
        await page.goto('https://web.txcs.tmall.com/login');
      }
      const browser = await chromium.connectOverCDP('http://localhost:9999');
      disposables.push(browser);
      const page = browser.contexts()[0].pages()[0];
      console.log(page.mainFrame().childFrames().length);
      console.log(await page.frameLocator('#alibaba-login-box').locator('#login').innerText());
    
      await Promise.all(disposables.map(d => d.close()));
    })();
  6. dgozman commented on Oct 26, 2022

    @dgozman
    Collaborator

    Investigation notes:

    • Page.getFrameTree does not return OOPIFs.
    • Target.attachedToTarget is dispatched for OOPIFs, but since we don't have parentFrameId there, we cannot attach a new frame to the frame tree, so we ignore this target.

    Fix ideas, upstream in Chromium:

    • Add parentTargetId and parentFrameId to TargetInfo.
    • Alternatively, Page.getFullFrameTree to get the full tree from the top-level page target, maybe with events that update it.
  7. added
    browser-chromium
    and removed on Oct 26, 2022
  8. pavelfeldman commented on Jun 30, 2023

    @pavelfeldman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

browser-chromiumv1.56

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions