Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Long-term support concerns with network interception due to Selenium Wire deprecation #3246

Closed
pradeepvegesna opened this issue Nov 6, 2024 · 2 comments
Labels
feature or fix already exists Upgrade to the latest version as needed question Someone is looking for answers workaround exists You can reach your destination if you do this...

Comments

@pradeepvegesna
Copy link

We have several use cases that require request and response interception for UI testing, and we’re considering using SeleniumBase for this purpose. However, I noticed that SeleniumBase's wire mode relies on Selenium Wire, which is deprecated since January 2024.

Although network interception is still functional through Selenium Wire, I’m concerned about the impact on long-term support. Could you provide guidance on the following:

Are there plans to update or replace Selenium Wire within SeleniumBase to ensure continued support for network interception?
Is it advisable to continue using the current setup, or would you recommend alternative approaches for network interception within SeleniumBase?
Any insights on future plans for handling network intercepts would be greatly appreciated.

@mdmintz mdmintz added question Someone is looking for answers workaround exists You can reach your destination if you do this... feature or fix already exists Upgrade to the latest version as needed labels Nov 6, 2024
@mdmintz
Copy link
Member

mdmintz commented Nov 6, 2024

The Selenium-Wire replacement is already included within SeleniumBase's new CDP Mode.

Here's an example of that, (SeleniumBase/examples/cdp_mode/raw_res_sb.py):

"""Using CDP.network.ResponseReceived and CDP.network.RequestWillBeSent."""
import colorama
import mycdp
import sys
from seleniumbase import SB

c1 = colorama.Fore.BLUE + colorama.Back.LIGHTYELLOW_EX
c2 = colorama.Fore.BLUE + colorama.Back.LIGHTGREEN_EX
cr = colorama.Style.RESET_ALL
if "linux" in sys.platform:
    c1 = c2 = cr = ""

async def send_handler(event: mycdp.network.RequestWillBeSent):
    r = event.request
    s = f"{r.method} {r.url}"
    for k, v in r.headers.items():
        s += f"\n\t{k} : {v}"
    print(c1 + "*** ==> RequestWillBeSent <== ***" + cr)
    print(s)

async def receive_handler(event: mycdp.network.ResponseReceived):
    print(c2 + "*** ==> ResponseReceived <== ***" + cr)
    print(event.response)

with SB(uc=True, test=True, locale_code="en") as sb:
    sb.activate_cdp_mode("about:blank")
    sb.cdp.add_handler(mycdp.network.RequestWillBeSent, send_handler)
    sb.cdp.add_handler(mycdp.network.ResponseReceived, receive_handler)
    url = "https://seleniumbase.io/apps/calculator"
    sb.cdp.open(url)
    sb.sleep(1)

Usage is different from regular Selenium-Wire, but it can do all the same things (and more) with better flexibility/control.

The greatest benefit for some people is that CDP Mode is part of UC Mode, which provides stealth to avoid bot-detection.
The old Selenium-Wire integration is not compatible with UC Mode, but CDP Mode patches the previous issues with Selenium-Wire that prevented compatibility.

Here's another example, (SeleniumBase/examples/cdp_mode/raw_req_sb.py), where specific requests were filtered out (intercepted and blocked), to prevent images from loading:

"""Using CDP.fetch.RequestPaused to filter content 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:  # Block the data (images)
        TIMED_OUT = mycdp.network.ErrorReason.TIMED_OUT
        s = f"BLOCKING | {r.method} | {r.url}"
        print(f" >>> ------------\n{s}")
        tab.feed_cdp(mycdp.fetch.fail_request(event.request_id, TIMED_OUT))

with SB(uc=True, test=True, locale_code="en") as sb:
    sb.activate_cdp_mode("about:blank")
    sb.cdp.add_handler(mycdp.fetch.RequestPaused, request_paused_handler)
    url = "https://gettyimages.com/photos/firefly-2003-nathan"
    sb.cdp.open(url)
    sb.sleep(5)

If people don't need the stealth features or the other improvements made to intercepting and handling network requests & responses, then they can continue using the existing Selenium-Wire integration as it currently stands. Or, if people want the upgrades, then the new integration is ready in CDP Mode, as shown above.

@mdmintz
Copy link
Member

mdmintz commented Nov 6, 2024

Closing this in favor of #3247.

@mdmintz mdmintz closed this as completed Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature or fix already exists Upgrade to the latest version as needed question Someone is looking for answers workaround exists You can reach your destination if you do this...
Projects
None yet
Development

No branches or pull requests

2 participants