-
Notifications
You must be signed in to change notification settings - Fork 1.4k
-
I can't replace the response body with my own HTML. I use this code but it doesn't work. Maybe it's the headers:
import time
import mycdp
from seleniumbase import SB
async def request_paused_handler(event, tab):
r = event.request
rid = event.request_id
# print(r.url)
if url == r.url:
print("Intercept!!!!!")
headers = [mycdp.fetch.HeaderEntry(name="Content-Type", value="text/html")]
tab.feed_cdp(mycdp.fetch.fulfill_request(
request_id=rid,
response_code=200,
response_headers=headers,
body="<html><body><h1>Hello World!</h1></body></html>",
))
else:
tab.feed_cdp(mycdp.fetch.continue_request(request_id=rid))
url = "https://example.com/"
with SB(uc=True, locale="en") as sb:
sb.set_window_size(1100, 800)
sb.activate_cdp_mode("about:blank")
sb.cdp.add_handler(mycdp.fetch.RequestPaused, request_paused_handler)
time.sleep(1)
sb.cdp.open(url)
sb.sleep(20)
I looked at all the available examples, but it didn't make things any clearer for me, sorry.
Beta Was this translation helpful? Give feedback.
All reactions
You can only do what the Chrome Devtools Protocol allows you to do.
https://chromedevtools.github.io/devtools-protocol/tot/Fetch/#method-continueRequest
https://chromedevtools.github.io/devtools-protocol/tot/Fetch/#method-fulfillRequest
If there's no existing example for what you're trying to do, you may need to experiment on your own.
Replies: 1 comment
-
You can only do what the Chrome Devtools Protocol allows you to do.
https://chromedevtools.github.io/devtools-protocol/tot/Fetch/#method-continueRequest
Screenshot
https://chromedevtools.github.io/devtools-protocol/tot/Fetch/#method-fulfillRequest
Screenshot 2025年04月11日 at 9 24 44 AM
If there's no existing example for what you're trying to do, you may need to experiment on your own.
Beta Was this translation helpful? Give feedback.