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

interceptor not work #437

Closed cooclzw closed 2 years ago

cooclzw commented 2 years ago

from seleniumwire import webdriver

firefox_capabilities = { "browserName": "firefox", "version": "93.0", }

sw_options = { 'addr': '127.0.0.1', 'port': 8087, 'auto_config': False, # Ensure this is set to False 'disable_capture': True } options = webdriver.FirefoxOptions()

browser = webdriver.Remote( "http://localhost:4444/wd/hub", options=options, desired_capabilities=firefox_capabilities, seleniumwire_options=sw_options )

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

browser.request_interceptor = interceptor browser.get("http://httpbin.org/headers") print(browser.page_source) browser.quit()

try this and result nothing change: "headers": { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,/;q=0.8", "Accept-Encoding": "gzip, deflate", "Accept-Language": "en-US,en;q=0.5", "Host": "httpbin.org", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:93.0) Gecko/20100101 Firefox/93.0", "X-Amzn-Trace-Id": "Root=1-61810acc-19644f264e1c301427d4b8a1" }

wkeeling commented 2 years ago

Thanks for raising this.

The interceptor won't fire if you have disable_capture: True in your sw_options. You should remove this option.

If you want to limit request capture, you can try switching to in-memory storage and reduce the number of requests stored using the request_storage_max_size option.

cooclzw commented 2 years ago

from seleniumwire import webdriver

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

firefox_capabilities = { "browserName": "firefox", "version": "93.0", }

sw_options = { 'addr': '127.0.0.1', 'port': 8087, 'auto_config': False, # Ensure this is set to False } options = webdriver.FirefoxOptions()

browser = webdriver.Remote( "http://localhost:4444/wd/hub", options=options, desired_capabilities=firefox_capabilities, seleniumwire_options=sw_options )

browser.request_interceptor = interceptor browser.get("http://httpbin.org/headers") print(browser.page_source) browser.quit()

t have tried this, but get the same result: "headers": { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,/;q=0.8", "Accept-Encoding": "gzip, deflate", "Accept-Language": "en-US,en;q=0.5", "Host": "httpbin.org", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:93.0) Gecko/20100101 Firefox/93.0", "X-Amzn-Trace-Id": "Root=1-618199ae-1ffa41e214610ad054a29fee" }

wkeeling commented 2 years ago

Apologies, I'd missed that you are using the Remote webdriver. When using Remote you need to point the browser back to Selenium Wire using the desired capabilities - for example:

sw_options = {
    'addr': '127.0.0.1',
    'port': 8087,
    'auto_config': False, # Ensure this is set to False
}

options = webdriver.FirefoxOptions()

firefox_capabilities = {
    "browserName": "firefox",
    "version": "93.0",
    "proxy": {
        "proxyType": "MANUAL",
        "httpProxy": "127.0.0.1:8087",
        "sslProxy": "127.0.0.1:8087",
    }
}

In the example above the browser is configured to point back to Selenium Wire on 127.0.0.1:8087 by means of the proxy in the firefox_capabilities dictionary. Obviously this assumes that Selenium Wire and the browser are both running on the same machine.

cooclzw commented 2 years ago

it Solved,thanks

brucez007 commented 2 years ago

Hello, I ran almost the same codes, but the issue still exists. I used webdriver.Firefox not webdriver.Remote. I'm using: selenium 4.0.0rc1 selenium-wire 4.5.3 in macOS Big Sur.

from seleniumwire import webdriver

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

sw_options = { 'addr': '127.0.0.1', 'port': 8087, 'auto_config': False, # Ensure this is set to False }

options = webdriver.FirefoxOptions()

firefox_capabilities = { "browserName": "firefox", "version": "94.0", "proxy": { "proxyType": "MANUAL", "httpProxy": "127.0.0.1:8087", "sslProxy": "127.0.0.1:8087", } }

options = webdriver.FirefoxOptions()

browser = webdriver.Firefox( options=options, desired_capabilities=firefox_capabilities, seleniumwire_options=sw_options )

browser.request_interceptor = interceptor browser.get("http://httpbin.org/headers") print(browser.page_source) browser.quit()

wkeeling commented 2 years ago

@brucez007 you only normally need to set auto_config: False if you're using webdriver.Remote. I see you're using webdriver.Firefox however. Is there a specific reason why you need auto_config: False?

brucez007 commented 2 years ago

@wkeeling No, I don't have special reason. Just because I'm a newbie for selenium, selenium-wire. I'm using webdriver.Firefox, but I didn't succeed in adding http header. I succeeded in adding http header when I used webdriver.chrome.

wkeeling commented 2 years ago

@brucez007 no problem.

In that case I would suggest removing the sw_options, firefox_capabilities and options altogether, since there is nothing specific that you need to pass in those, which simplifies your code to:

from seleniumwire import webdriver

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

browser = webdriver.Firefox()

browser.request_interceptor = interceptor
browser.get("http://httpbin.org/headers")
print(browser.page_source)
browser.quit()

And you'll need to be running version 4.5.4 of Selenium Wire.

brucez007 commented 2 years ago

@wkeeling After I updated Selenium Wire to 4.5.4, I ran above codes you have posted, the issue still exists. $ pip list | grep sele selenium 4.0.0rc1 selenium-wire 4.5.4 $ python3 --version Python 3.9.7

It returned : "headers": { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,/;q=0.8", "Accept-Encoding": "gzip, deflate", "Accept-Language": "en-US,en;q=0.5", "Host": "httpbin.org", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:94.0) Gecko/20100101 Firefox/94.0", "X-Amzn-Trace-Id": "Root=1-618bae96-656501e712e00b0a468d2bdb" }

wkeeling commented 2 years ago

@brucez007 ok, thanks. Can you also make sure you're running the following versions of selenium and geckodriver:

selenium 4.0.0
geckodriver 0.30.0
brucez007 commented 2 years ago

@wkeeling Yes, selenium 4.0.0rc1

$ geckodriver -V geckodriver 0.30.0

wkeeling commented 2 years ago

@brucez007 can you use version 4.0.0 specifically as I believe there are changes between 4.0.0rc1 and 4.0.0

brucez007 commented 2 years ago

@wkeeling Wow, you are quite right. After I installed Selenium to version 4.0.0, the customized Header appeared. Thanks!!!

"headers": { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,/;q=0.8", "Accept-Encoding": "gzip, deflate", "Accept-Language": "en-US,en;q=0.5", "Host": "httpbin.org", "New-Header": "Some Value", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:94.0) Gecko/20100101 Firefox/94.0", "X-Amzn-Trace-Id": "Root=1-618bbda3-2edcb1c93bad7b8f70fede3a" }