How to add a request header at selenium-wire as passed argument?

979 Views Asked by At

What I need is to set header values defined outside def interceptor(request) function. How can I pass it?

def randomkeklul(main_arg):
    return random.choice(['kek', 'lul']), random.choice(main_arg)

def interceptor(request):
    request.headers['Accept-Encoding'] = value1
    request.headers['Accept-Language'] = value2

def main():
    main_arg = '12345'
    value1, value2 = randomkeklul(main_arg)
    driver.request_interceptor = interceptor

It doesn't help https://github.com/wkeeling/selenium-wire#example-add-a-request-header It works as driver.request_interceptor = interceptor but if I try to add arguments so it looks like

def randomkeklul(main_arg):
    return random.choice(['kek', 'lul']), random.choice(main_arg)

def interceptor(request,arg1,arg2):
    request.headers['Accept-Encoding'] = arg1
    request.headers['Accept-Language'] = arg2

def main():
    main_arg = '12345'
    value1, value2 = randomkeklul(main_arg)
    driver.request_interceptor = interceptor(arg1=value1, arg2=value2)

it raises error

TypeError: interceptor() missing 1 required positional argument: 'request'

1

There are 1 best solutions below

0
On

I have the same issue. The only think that works fine for me is to get the parameters from some other function/file:

def interceptor(request):
    from custom_credentials import custom_credentials
    username, password = custom_credentials()
    
    auth_b = username + ':' + password
    
    auth = (
        base64.encodebytes(auth_b.encode())
            .decode()
            .strip()
    )
    
    del request.headers['Authorization']  
    request.headers['Authorization'] = f'Basic {auth}'