0

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.

iChux
  • 2,266
  • 22
  • 37
juanmac
  • 121
  • 1
  • 12
  • I think Selenium-Wire mirrors Selenium's requests... you're leaving out some key bits of your code. Namely the .get()s used. Add those to your post. Also remember that Selenium itself will be running on localhost. (that's the bit that receives/sends requests from/to the browser... webdriver <-> browser.) – pcalkins Jun 15 '22 at 18:40
  • @pcalkins thanks for your answer. I do not have any request made from this code, i'm using selenium to monitor some request made from a web browser that's already running. These requests are registered by selenium when I navigate (through the browser itself) to , for example, google.com, but when the requests are made to get a local file (also from the browser, not the code you are seeing) these are ignored, and those are the ones I need to monitor. – juanmac Jun 22 '22 at 15:34
  • local is a different domain and possibly different window handle. Without seeing the code I can't help much, but Selenium can switch to different handles (and get them). – pcalkins Jun 22 '22 at 17:28
  • @pcalkins i do not get what you mean by "the rest of the code" this is the whole code of my tiny selenium app. I do simply want to intercept the local requests of my browser when gettin a missing file. – juanmac Jun 23 '22 at 20:12
  • the only get() I see is for google.com. – pcalkins Jun 23 '22 at 20:28
  • @pcalkins that get is an example, when I uncomment it, it redirects successfully yo google and starts registering incoming requests as I want. What I really want is to register requests that aim to localhost and are not set by selenium. However, if I replace the .get(google) line with the desired url to intercept (localhost:3000/screen/1), it does not recognize the requests either. again, those request are retrieving local mp4 files to reproduce them in browser – juanmac Jun 24 '22 at 13:27
  • 1
    Sounds like you've got three servers running on localhost here... (selenium between webdriver and browser, wire between browser and internet, and then your own server) Make sure there's no port conflicts or anything in your hosts file that specifies a certain ip/port. You might also check this post which discusses the browser bypassing wire for localhost: https://github.com/wkeeling/selenium-wire/issues/326 – pcalkins Jun 24 '22 at 16:26

1 Answers1

0

Solved it.

Just had driver.request_interceptor = interceptorFunction.

Thank's @pcalkins

juanmac
  • 121
  • 1
  • 12