2

I want to change my header Http information but nothing happens with the below code. Please help me, what is the problem and how should I solve it? I want to be able to change the user's information on each visit, like the ModHeader extension.

import time 
  import os
  from seleniumwire import webdriver
  from selenium.webdriver.chrome.options import Options  # Import from seleniumwire
  driver = webdriver.Chrome()
  def interceptor(request):
   request.headers['User-Agent'] = 'Mozilla/6.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36'
   request.headers['sec-ch-ua'] = '"Google Chrome";v="93", " Not;A Brand";v="99", "Chromium";v="93"'
   request.headers['sec-ch-ua-mobile'] = '?0'
   request.headers['sec-ch-ua-platform'] = 'os'
driver.request_interceptor = interceptor
driver.get("https://google.com")
for request in driver.requests:
  print(request.headers)
  print(request.response)
time.sleep(500)

my problem like this link in my driver data shows this

204
sec-ch-ua: "Google Chrome";v="111", "Not(A:Brand";v="8", "Chromium";v="111"
sec-ch-ua-mobile: ?0
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36
sec-ch-ua-arch: "x86"
sec-ch-ua-full-version: "111.0.5563.64"
sec-ch-ua-platform-version: "10.0.0"
sec-ch-ua-full-version-list: "Google Chrome";v="111.0.5563.64", "Not(A:Brand";v="8.0.0.0", "Chromium";v="111.0.5563.64"
sec-ch-ua-bitness: "64"
sec-ch-ua-model:
sec-ch-ua-wow64: ?0
sec-ch-ua-platform: "Windows"
accept: image/avif,image/webp,image/apng,image/svg+xml,image/,/*;q=0.8
x-client-data: CIv0ygE=
sec-fetch-site: same-origin
sec-fetch-mode: no-cors
sec-fetch-dest: image
referer: https://www.google.com/
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9
cookie: 1P_JAR=2023-03-12-22; AEC=ARSKqsLIZuIQCQoYlTRZfCg_tawXYvRiB1kDB8z1c0VRRrPAuD-iymrB8A; NID=511=rcB6Nv8Pzk7ZBKQzTcVBvMSzLfv0oqUwXU9b8LzBUfGVVEt1vkRAlZ5Lkg6fnKFbfb8jcf6tsYPVWRSe0wmd932NmGYf1vrDsxPp8nBEegoW_5nqEU2OCrKzLqDrBaBpsfeLOiXnJoz1ZJC6Cd2tqiO6XhTufWslhtromMGSRZc
User-Agent: Mozilla/6.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36
sec-ch-ua: "Google Chrome";v="93", " Not;A Brand";v="99", "Chromium";v="93"
sec-ch-ua-mobile: ?0
  • The Selenium Wire docs say you must delete first: "If you wish to replace a header, make sure you delete the existing header first with del request.headers['header-name'], otherwise you’ll create a duplicate." – pcalkins Mar 13 '23 at 23:46

1 Answers1

1

i use this code

from seleniumwire import webdriver  # Import from seleniumwire
import time
# Create a new instance of the Chrome driver (or Firefox)
driver = webdriver.Chrome()

# Create a request interceptor
def interceptor(request):
    #del request.headers['Referer']  # Delete the header first
    del request.headers['sec-ch-ua-mobile']  # Delete the header firs
    request.headers['sec-ch-ua-mobile'] = '?1'

# Set the interceptor on the driver
driver.request_interceptor = interceptor
# All requests will now use 'some_sec' for the user-agent
driver.get('https://httpbin.org/anything')
time.sleep(100)

and i get this answer

{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {}, 
  "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.7", 
    "Accept-Encoding": "gzip, deflate, br", 
    "Accept-Language": "en-US,en;q=0.9", 
    "Host": "httpbin.org", 
    "Sec-Ch-Ua": "\"Google Chrome\";v=\"111\", \"Not(A:Brand\";v=\"8\", \"Chromium\";v=\"111\"", 
    "Sec-Ch-Ua-Mobile": "?1", 
    "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/111.0.0.0 Safari/537.36", 
    "X-Amzn-Trace-Id": "Root=1-6411d437-6a5b738d424453642abefa89"
  }, 
  "json": null, 
  "method": "GET", 
  "origin": "5.239.189.35", 
  "url": "https://httpbin.org/anything"
}

enter image description here But unfortunately, there is no change in Google settings. Where does the problem seem to come from?

  • You just confirmed yourself that it works as intended. The header still is shown different in the network-tab, because it gets changed after the request exits your browser. – kaliiiiiiiii Mar 15 '23 at 16:53
  • @kaliiiiiiiii how i can change header like in the photo . i see some chrome extension can change headr http. like RequestInterceptor .but i dont now how i can use this way .this extension have not api so i look up new way in selenium python. or create api for use this extension. i saw ago your question .how you solved this problem? – masoud fallah Mar 15 '23 at 21:15
  • 1
    It allready does change, it just isn't shown in the nework-tab. Alternatively you could try using modheader-selenium or selenium-interceptor. – kaliiiiiiiii Mar 16 '23 at 06:59