question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Firefox bypasses Selenium Wire for localhost addresses

See original GitHub issue

client:

from selenium.webdriver import FirefoxProfile
from seleniumwire import webdriver

GECKO_PATH = './drivers/geckodriver'

def interceptor(request):
    del request.headers['User-Agent']
    request.headers['User-Agent'] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36"


def init_driver():
    options = dict()
    profile = FirefoxProfile()
    driver = webdriver.Firefox(
        executable_path=GECKO_PATH,
        seleniumwire_options=options,
        firefox_profile=profile
    )

    return driver


def main():
    driver = init_driver()
    driver.request_interceptor = interceptor
    driver.get('http://127.0.0.1:8000/')
    driver.quit()


if __name__ == '__main__':
    main()

service to print headers:

from sanic import Sanic
from sanic.response import json

app = Sanic("My Hello, world app")


@app.route('/')
async def test(request):
    print('-'*30)
    for k, v in dict(request.headers).items():
        print(f'"{k}": "{v}"')
    print('-'*30)

    return json({'hello': 'world'})


if __name__ == '__main__':
    app.run()

printed headers here:

------------------------------
"host": "127.0.0.1:8000"
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:88.0) Gecko/20100101 Firefox/88.0"
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
"accept-language": "en-US,en;q=0.5"
"accept-encoding": "gzip, deflate"
"connection": "keep-alive"
"upgrade-insecure-requests": "1"
------------------------------

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
BinaryCanoncommented, Jun 21, 2021

@wkeeling Thanks, I understand now. Using http://httpbin.org/headers indeed display it as you mentioned.

0reactions
jaymc-argcommented, Jun 29, 2022

Chrome actually does the same thing and we’ve disabled it by default when you use the ChromeDriver, but we haven’t done it for Firefox. I’ll look at adding that in.

Thanks again!

Hi @wkeeling I’m trying to intercept localhost request for a local file form a Chrome/Chromium browser but selenium-wire ignores localhost requests. Can you explain a bit more please? This is what I’m trying:

import time
from seleniumwire import webdriver
chrome_options = webdriver.ChromeOptions()
try:
    driver = webdriver.Chrome(
        chrome_options=chrome_options
    )
except Exception as e:
    print(e)

driver.get('http://0.0.0.0:3000/screen/a') #webpage I want to monitor

while(True):
        for request in driver.requests:
                if request.response:
                if request.response.status_code == 200:
                        print(
                        request.url,
                        request.response.status_code,
                        'Error')
                        driver.refresh()
                        time.sleep(5) 

The requests made are something like this htttp://localhost:3000/media/filename.mp4 Here is a thread in stackoverflow I’ve created

Thanks in advance.

Read more comments on GitHub >

github_iconTop Results From Across the Web

selenium-wire - PyPI
Selenium Wire extends Selenium's Python bindings to give you access to the ... Prevent Firefox from bypassing Selenium Wire for localhost addresses.
Read more >
How to run Firefox headless in seleniumwire? - Stack Overflow
I had to check into the seleniumwire files, this combination worked for me from selenium.webdriver import FirefoxOptions from seleniumwire import webdriver ...
Read more >
How to Handle SSL Certificate in Selenium WebDriver with ...
Have you ever wondered why this happens? The answer is simple, on manually opening a URL, the browser automatically imports the required ...
Read more >
selenium-wire Changelog - pyup.io
Prevent Firefox from bypassing Selenium Wire for localhost addresses. * Fix bug where DNS wasn't being resolved through the proxy for socks5h.
Read more >
WebDriverManager - Boni García
Selenium WebDriver is a library that allows controlling web browsers programmatically. It provides a cross-browser API that can be used to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found