wkeeling / selenium-wire

Extends Selenium's Python bindings to give you the ability to inspect requests made by the browser.
MIT License
1.9k stars 251 forks source link

Remote Webdriver not capturing requests #644

Open hnandiwada opened 1 year ago

hnandiwada commented 1 year ago

We are running a Selenium Grid with a Chrome node in Docker. Our docker-compose looks like this:

version: "3"
services:
  chrome:
    image: seleniarm/node-chromium:latest
    shm_size: 2gb
    depends_on:
      - seleniarm-hub
    environment:
      - SE_EVENT_BUS_HOST=seleniarm-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
      - SE_NODE_MAX_SESSIONS=10
      - SE_NODE_MAX_INSTANCES=10
    ports:
      - 5555:5555

  seleniarm-hub:
    image: seleniarm/hub:latest
    container_name: seleniarm-hub
    ports:
      - "4442:4442"
      - "4443:4443"
      - "4444:4444"

(I am using an M1 Mac, hence seleniarm instead of selenium.) On the local machine, we create a remote driver and it seems that we successfully navigate to a URL. However, the requests attached to the driver always come up empty. Code on the machine:

    options = webdriver.ChromeOptions()
    options.headless = True
    cap = DesiredCapabilities.CHROME
    cap["goog:loggingPrefs"] = {"performance": "ALL"}
    seleniumwire_options = {
        "addr": "127.0.0.1",
        # "addr": "0.0.0.0",
        "auto_config": False,
        "port": 5555,
    }
    driver = webdriver.Remote(
        # command_executor=f"{HUB_URL}:4444/wd/hub",
        command_executor=HUB_URL, # <---- "http://localhost:4444/wd/hub"
        # command_executor="http://selenium-hub:444/wd/hub",
        options=options,
        desired_capabilities=cap,
        seleniumwire_options=seleniumwire_options,
    )
    driver.get(url)
    time.sleep(3)
    requests = driver.requests # <---- showing up as empty list

We confirmed that driver.current_url is the same as the URL we want to query. When we use the Chrome driver without Docker, everything works perfectly as expected:

    driver = webdriver.Chrome(
        options=options, service=chromeService(ChromeDriverManager().install())
    )

We've tried a whole lot of flags to try to get this to work with no dice. How can we make sure we're sniffing the network?

hnandiwada commented 1 year ago

Docker logs:

seleniarm-hub     | 21:13:30.080 INFO [LocalDistributor.newSession] - Session request received by the Distributor:
seleniarm-hub     |  [Capabilities {browserName: chrome, goog:chromeOptions: {args: [--disable-gpu, --disable-extensions, --disable-infobars, --start-maximized, --disable-notifications, --headless, --no-sandbox, --disable-dev-shm-usage, proxy-bypass-list=<-loopback>], extensions: []}, goog:loggingPrefs: {performance: ALL}, pageLoadStrategy: normal}]
voyager-chrome-1  | Starting ChromeDriver 109.0.5414.74 (e7c5703604daa9cc128ccf5a5d3e993513758913-refs/branch-heads/5414@{#1172}) on port 6139
voyager-chrome-1  | Only local connections are allowed.
voyager-chrome-1  | Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
voyager-chrome-1  | ChromeDriver was started successfully.
voyager-chrome-1  | [1674854010.117][SEVERE]: bind() failed: Cannot assign requested address (99)
voyager-chrome-1  | 21:13:30.355 INFO [LocalNode.newSession] - Session created by the Node. Id: bd9134d041a87d7ad71a5c21374d18ae, Caps: Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 109.0.5414.74, chrome: {chromedriverVersion: 109.0.5414.74 (e7c5703604da..., userDataDir: /tmp/.org.chromium.Chromium...}, goog:chromeOptions: {debuggerAddress: localhost:42739}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: LINUX, proxy: Proxy(), se:cdp: http://localhost:42739, se:cdpVersion: 109.0.5414.74, se:vncEnabled: true, se:vncLocalAddress: ws://172.19.0.3:7900, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}
seleniarm-hub     | 21:13:30.358 INFO [LocalDistributor.newSession] - Session created by the Distributor. Id: bd9134d041a87d7ad71a5c21374d18ae
seleniarm-hub     |  Caps: Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 109.0.5414.74, chrome: {chromedriverVersion: 109.0.5414.74 (e7c5703604da..., userDataDir: /tmp/.org.chromium.Chromium...}, goog:chromeOptions: {debuggerAddress: localhost:42739}, goog:loggingPrefs: {performance: ALL}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: LINUX, proxy: {}, se:bidiEnabled: false, se:cdp: ws://172.19.0.3:4444/sessio..., se:cdpVersion: 109.0.5414.74, se:vnc: ws://172.19.0.3:4444/sessio..., se:vncEnabled: true, se:vncLocalAddress: ws://172.19.0.3:7900, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}