Skip to content

Commit

Permalink
Add CDP Mode example that modifies requests
Browse files Browse the repository at this point in the history
  • Loading branch information
mdmintz committed Dec 22, 2024
1 parent ffeee7f commit 05bb565
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions examples/cdp_mode/raw_req_mod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Using CDP.fetch.RequestPaused to modify requests in real-time."""
import mycdp
from seleniumbase import SB


async def request_paused_handler(event, tab):
r = event.request
is_image = ".png" in r.url or ".jpg" in r.url or ".gif" in r.url
if not is_image: # Let the data through
tab.feed_cdp(mycdp.fetch.continue_request(request_id=event.request_id))
else: # Modify the data (change the image)
tab.feed_cdp(mycdp.fetch.continue_request(
request_id=event.request_id,
url="https://seleniumbase.io/other/with_frakes.jpg"
))


with SB(uc=True, test=True, locale_code="en", pls="none") as sb:
sb.activate_cdp_mode("about:blank")
sb.cdp.add_handler(mycdp.fetch.RequestPaused, request_paused_handler)
sb.cdp.open("https://gettyimages.com/photos/jonathan-frakes-cast-2022")
new_size = "--width:100;--height:100;"
sb.cdp.set_attributes('[style*="--width:"]', "style", new_size)
sb.sleep(6)

0 comments on commit 05bb565

Please sign in to comment.