Comments (6)
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.
test again with latest version
from python-cdp.
seems working on your new snippet.
will check it later also on a more complex code.
from python-cdp.
problem still exists and not fixed.
tested on latest (for the time of this post) CDP version
Steps to reproduce this bug:
- update CDP protocol files used for generating
cdp
folder (you can use my .sh file for one-command execution) - 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.
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:
- Clone this repository
- Replace the
browser_protocol.json
andjs_protocol.json
files atpycdp/gen
folder by the CDP protocol version you wish, then runpython pycdp/gen/generate.py
, it will refresh builtincdp
wrapper. - 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.
Here was a cdpgen-generated package usage misunderstood.
cdpgen generated package cannot be used separate from pycdp due to being a part of it.
from python-cdp.
Related Issues (6)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-cdp.