Search code examples
seleniumrequestseleniumwire

Selenium-wire | Monitor localhost requests


I'm trying to intercept localhost requests of my Chromium browser with selenium-wire but it does not detect them.

This is my code:

import time

from selenium.common.exceptions import WebDriverException
from seleniumwire import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option(
    "debuggerAddress", "127.0.0.1:9222"
)  # I'm controling an existing Chromium session

sw_options = {"port": 12345}  # Option required by selenium-wire existing sessions

try:
    driver = webdriver.Chrome(
        executable_path="./chromedriver",
        chrome_options=chrome_options,
        seleniumwire_options=sw_options,
    )

    driver.get(
        "https://www.google.com"
    )  # ==> if I uncomment this line it works fine, it starts registrating the incoming requests

    while True:
        for request in driver.requests:
            if request.response:
                if request.response.status_code == 206:
                    print(request.url, request.response.status_code, "Error")
                    driver.refresh()
                    time.sleep(5)
except WebDriverException as exc:
    print(exc.msg)

The problem is that the requests I'm trying to monitor are some that GET a local video file and plays it in the browser in a url like htttp://localhost:3000/media/filename.mp4.

Requests are effectively made:

enter image description here

But it seems that localhost requests are ignored by selenium-wire...

Any clue on how can this be done? What I really want is to refresh the browser when it can't find the video (ResponseStatus: 400), so maybe there is another way to accomplish this other than monitoring incoming requests.


Solution

  • Solved it.

    Just had driver.request_interceptor = interceptorFunction.

    Thank's @pcalkins