Skip to content

Commit 05bb565

Browse files
committed
Add CDP Mode example that modifies requests
1 parent ffeee7f commit 05bb565

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

examples/cdp_mode/raw_req_mod.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""Using CDP.fetch.RequestPaused to modify requests in real-time."""
2+
import mycdp
3+
from seleniumbase import SB
4+
5+
6+
async def request_paused_handler(event, tab):
7+
r = event.request
8+
is_image = ".png" in r.url or ".jpg" in r.url or ".gif" in r.url
9+
if not is_image: # Let the data through
10+
tab.feed_cdp(mycdp.fetch.continue_request(request_id=event.request_id))
11+
else: # Modify the data (change the image)
12+
tab.feed_cdp(mycdp.fetch.continue_request(
13+
request_id=event.request_id,
14+
url="https://seleniumbase.io/other/with_frakes.jpg"
15+
))
16+
17+
18+
with SB(uc=True, test=True, locale_code="en", pls="none") as sb:
19+
sb.activate_cdp_mode("about:blank")
20+
sb.cdp.add_handler(mycdp.fetch.RequestPaused, request_paused_handler)
21+
sb.cdp.open("https://gettyimages.com/photos/jonathan-frakes-cast-2022")
22+
new_size = "--width:100;--height:100;"
23+
sb.cdp.set_attributes('[style*="--width:"]', "style", new_size)
24+
sb.sleep(6)

0 commit comments

Comments
 (0)