Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Substitution of the response body for a request #3669

Answered by mdmintz
alex4845 asked this question in Q&A
Discussion options

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.

You must be logged in to vote

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

Comment options

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.

You must be logged in to vote
0 replies
Answer selected by mdmintz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /