Switching from Playwright to Pydoll, need some help with features #298
-
|
Hi, I recently came across this project and I like the customization depth of the browser (especially in regards to canvas fingerprinting and other fingerprinting techniques) I have some questions about transitioning my project from Playwright to Pydoll though, as it seems more complex to do some things within Pydoll than in Playwright:
This makes it so that when the browser clicks some element, it expects a network response and is able to receive the body of the response for other uses. Doing this in Pydoll seems a bit more difficult and I'm not sure if it's possible to do this feature at all (expecting a response when doing some specific action). I appreciate any help and can provide more info/examples for clarification if needed. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
Hello @sadikhan918.
from pydoll.browser.options import ChromiumOptions
from pydoll.browser.chrome import Chrome
options = ChromiumOptions()
options.add_argument("--user-data-dir=/path/to/persistent/storage")
chrome = Chrome(options)
await chrome.start() # this will launch with the persistent context! For network interception, you need to enable the I'll add this same pattern to pydoll eventually, but for now you can do this: # you can just copy this context manager!
@asynccontextmanager
async def expect_response(tab, url_pattern: str, timeout: float = 10.0):
event_fired = asyncio.Event()
response_storage = {"data": None, "error": None}
async def response_callback(event_details: ResponseReceivedEvent):
response_url = event_details['params']['response']['url']
if url_pattern in response_url:
request_id = event_details['params']['requestId']
try:
body = await tab.get_response_body(request_id)
response_storage['data'] = {
"url": response_url,
"status": event_details['params']['response']['status'],
"headers": event_details['params']['response']['headers'],
"body_json": body.json() if body.is_json else None,
"body_text": body.text,
}
except Exception as e:
response_storage['error'] = f"Error: {e}"
finally:
event_fired.set()
await tab.enable_network_events()
callback_id = await tab.on(NetworkEvent.RESPONSE_RECEIVED, response_callback)
try:
yield response_storage
await asyncio.wait_for(event_fired.wait(), timeout=timeout)
except asyncio.TimeoutError:
response_storage['error'] = f"Timeout{timeout}"
finally:
await tab.remove_callback(callback_id)
# now you can use like that:
async with expect_response(tab, 'api/someApiEndpoint', timeout=10) as response:
await (await tab.find(id='someElement')).click()Please note that you only need to copy this context manager. I'll add to pydoll soon. Thanks for the feedback! |
Beta Was this translation helpful? Give feedback.
Hello @sadikhan918.
For network interception, you need to enable the
network_events. There's a entire section in the documentation just for that:https://pydoll.tech/docs/features/network/monitoring/
I'll add this same pattern to pydoll eventually, but for now you can do this:
# you can just copy thi…