GithubHelp home page GithubHelp logo

Comments (6)

HMaker avatar HMaker commented on May 31, 2024

It's probably caused by race conditions, the page might be loading too fast between the cdp.page.navigate() command and the async with target_session.wait_for() subscription. We need to subscribe to the event before it be triggered.

from python-cdp.

HMaker avatar HMaker commented on May 31, 2024

test again with latest version

from python-cdp.

TurboKach avatar TurboKach commented on May 31, 2024

seems working on your new snippet.
will check it later also on a more complex code.

from python-cdp.

TurboKach avatar TurboKach commented on May 31, 2024

problem still exists and not fixed.
tested on latest (for the time of this post) CDP version

Steps to reproduce this bug:

  1. update CDP protocol files used for generating cdp folder (you can use my .sh file for one-command execution)
  2. Double check that you are NOT using default pycdp lib protocol classes, but generated a fresh ones from pycdp import cdp ---> import cdp

Here is my code that you can use for check:

import asyncio
import sys
import cdp
from pycdp.browser import ChromeLauncher
from pycdp.asyncio import connect_cdp


async def main():
    platform: sys.platform = sys.platform
    chrome_binary_path = '/usr/bin/google-chrome'
    if platform == "darwin":
        chrome_binary_path = (
            "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"  # Mac chrome path
        )
    elif platform in ["linux", "linux2"]:
        chrome_binary_path = "/usr/bin/google-chrome"  # Linux chrome path
    elif platform == "windows":
        chrome_binary_path = "C:\Program Files\Google\Chrome\Application\chrome.exe"  # windows path
    chrome = ChromeLauncher(
        binary=chrome_binary_path,  # linux path
        args=['--remote-debugging-port=9222',]
    )
    # ChromeLauncher.launch() is blocking, run it on a background thread
    await asyncio.get_running_loop().run_in_executor(None, chrome.launch)
    conn = await connect_cdp('http://localhost:9222')
    target_id = await conn.execute(cdp.target.create_target('about:blank'))
    target_session = await conn.connect_session(target_id)
    await target_session.execute(cdp.page.enable())
    # you may use "async for target_session.listen()" to listen multiple events, here we listen just a single event.
    with target_session.safe_wait_for(cdp.page.DomContentEventFired) as navigation:
        await target_session.execute(cdp.page.navigate('https://chromedevtools.github.io/devtools-protocol/'))
        await navigation
    print("EVENT FIRED")
    dom = await target_session.execute(cdp.dom.get_document())
    node = await target_session.execute(cdp.dom.query_selector(dom.node_id, 'p'))
    js_node = await target_session.execute(cdp.dom.resolve_node(node))
    print((await target_session.execute(
        cdp.runtime.call_function_on('function() {return this.innerText;}', js_node.object_id, return_by_value=True)))[
              0].value)
    await target_session.execute(cdp.page.close())
    await conn.close()
    await asyncio.get_running_loop().run_in_executor(None, chrome.kill)


asyncio.run(main())

from python-cdp.

HMaker avatar HMaker commented on May 31, 2024

I tried to reproduce with your steps but cdp.page.DomContentEventFired is still fired. You can't generate a new, external cdp package, you need to refresh the builtin one:

  1. Clone this repository
  2. Replace the browser_protocol.json and js_protocol.json files at pycdp/gen folder by the CDP protocol version you wish, then run python pycdp/gen/generate.py, it will refresh builtin cdp wrapper.
  3. Run your test script again.

This is the output I got from your test script

NAVIGATION FINISHED
EVENT FIRED
The Chrome DevTools Protocol allows for tools to instrument, inspect, debug and profile Chromium, Chrome and other Blink-based browsers. Many existing projects currently use the protocol. The Chrome DevTools uses this protocol and the team maintains its API.

I just added a new print at the end safe_wait_for() block to show it entered the block. I used Chrome 110 on Ubuntu.

from python-cdp.

TurboKach avatar TurboKach commented on May 31, 2024

Here was a cdpgen-generated package usage misunderstood.
cdpgen generated package cannot be used separate from pycdp due to being a part of it.

#4

from python-cdp.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.