-1

I am using Selenium and Selenium-wire in Python. To amend headers in the webdriver, do we use request_intercept in Selenium-wire and ChromeOptions in Selenium?

Example to change refers
Selenium-wire

from seleniumwire import webdriver
driver = webdriver.Chrome()

def intercept(req):
    del request.headers['Refers']
    request.headers['Refers']='https://www.google.com/'
driver.request_interceptor= intercept

driver.get('URL')

Selenium

from selenium import webdriver

opt = webdriver.ChromeOptions()

r='https://www.google.com/'
opt.add_argument(f'refers={r}')

driver = webdriver.Chrome(options=opt)

123456
  • 393
  • 3
  • 12

1 Answers1

2

Selenium chromeoptions simply cannot do this. Per the ChromeOptions documentation, the options does not include any functionality such as you've written in the second block of code.

I do believe that seleniumwire is the best way to accomplish this (it's designed exactly for this), and it's pretty simple. Do note that the webdriver import MUST be from selenium wire for this to work, as in your first example. Functionality from pure selenium must be imported specifically (eg from selenium.webdriver.support.ui import WebDriverWait).

Selenium-wire does indeed have its own set of options, passed in something like this:

driver = webdriver.Chrome(seleniumwire_options=options)

but even these do not include header modification.

So, I think your best bet is to continue using wire for request/response-specific actions and testing while adding from raw Selenium exactly what else you may desire.

C. Peck
  • 3,641
  • 3
  • 19
  • 36