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

Custom headers are not always present #266

Closed eqMFqfFd closed 3 years ago

eqMFqfFd commented 3 years ago

Issue description

It strikes me that when I add custom headers to Chrome browser using wkeeling/selenium-wire, the headers sometimes apply and sometimes do not.

Code to reproduce

from seleniumwire import webdriver
custom_headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.1 Safari/605.1.15"
}

driver = webdriver.Chrome()

def request_interceptor(request) : 
    request.headers = custom_headers; 

driver.request_interceptor = request_interceptor;
driver.get(url)
print(driver.page_source)

Tests

Local test

https://bit.ly/3da3X1S

Chrome automation test

https://bit.ly/3mIbS9H

eqMFqfFd commented 3 years ago

It is worth noting that using the undetected-chromedriver module, the browser name successfully updates (but not to the custom browser set).

Without also deviating from the central topic, what about these red indicators using the module? Will this affect tests in other bot-protected sites?

wkeeling commented 3 years ago

Thanks for raising this.

This is possibly happening because you're assigning a dictionary to request.headers and overwriting the dictionary-like object that holds the existing headers.

To replace an existing header such as the user-agent, you'll need to remove it first and then add it:

def request_interceptor(request) : 
    del request.headers["User-Agent"]
    request.headers["User-Agent"] = custom_headers["User-Agent"]
eqMFqfFd commented 3 years ago

Thank you.