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 254 forks source link

I cannot add a simple header #444

Closed farausername closed 2 years ago

farausername commented 2 years ago

Hi guys,

I don't know why it doesn't allow me to add a simple header. I have Chrome: Version 95.0.4638.69 (Official Build) (64-bit) selenium-wire 4.5.4

I've fiddled with sw_options a bit without any success. This is my attempt:

The indent on line driver.request_interceptor = interceptor was just a typo that is not present in the script.

import time
from seleniumwire import webdriver

PATH = "./chromedriver.exe"

sw_options = {     
 # 'disable_capture': False,      
 # 'auto_config': False,     
 # 'disable_encoding': True      
 # 'disable_capture': True  
}

driver = webdriver.Chrome(PATH,  seleniumwire_options=sw_options)

def interceptor(request):  
    request.headers['New-Header'] = 'Some Value'
driver.request_interceptor = interceptor

driver.get('https://httpbin.org/headers')
time.sleep(60)

This is the response:

{
 "headers": {
   "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
   "Accept-Encoding": "gzip, deflate, br",
   "Accept-Language": "en-US,en;q=0.9",
   "Host": "httpbin.org",
   "Sec-Ch-Ua": "\"Google Chrome\";v=\"95\", \"Chromium\";v=\"95\", \";Not A Brand\";v=\"99\"",
   "Sec-Ch-Ua-Mobile": "?0",
   "Sec-Ch-Ua-Platform": "\"Windows\"",
   "Sec-Fetch-Dest": "document",
   "Sec-Fetch-Mode": "navigate",
   "Sec-Fetch-Site": "none",
   "Sec-Fetch-User": "?1",
   "Upgrade-Insecure-Requests": "1",
   "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36",
   "X-Amzn-Trace-Id": "Root=1-618bb105-61b21fde22d06dd75b69d083"
 }

}

farausername commented 2 years ago

strange, in linux it works fine.

wkeeling commented 2 years ago

Can you confirm what version of Selenium and chromedriver you're using on Windows?

farausername commented 2 years ago

selenium==3.141.0 selenium-wire==4.5.4

C:\00workspace >chromeDriver -v ChromeDriver 94.0.4606.41 (333e85df3c9b656b518b5f1add5ff246365b6c24-refs/branch-heads/4606@{#845})

wkeeling commented 2 years ago

Thanks. Would you be able to upgrade Selenium to the latest version (4.0.0) to see if that fixes?

pip install selenium==4.0.0
farausername commented 2 years ago

I've updated it to 4.0.0. It didn't inject the new header field. I also updated chromedriver to version 95.xx, but it still does not inject it.

But at least it works ok in Linux.

wkeeling commented 2 years ago

OK thanks for trying. I'll try and reproduce on Windows.

wkeeling commented 2 years ago

@farausername one last thing to mention - it's possible that Selenium Wire is being prevented from modifying Chrome's proxy configuration (it needs to do this in order to intercept requests), by e.g. some security policy or something else. Can you check your Windows proxy configuration and see whether there is already a proxy configured and whether it allows you to deactivate/change it? You should be able to find that by searching for "proxy" from the Start menu.

farausername commented 2 years ago

Proxy settings in Windows 10 say: *some of these settings are hidden or managed by your organization. It's my work laptop, so your assumption might be correct.

wkeeling commented 2 years ago

OK, sounds like the proxy settings are being locked down by your organization, probably so that they cannot be tampered with. That would likely explain why it's not working on Windows. If you want to override those you'll need local admin permission on the machine. Thanks for checking - it's good to have a plausible explanation!