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

got the proxy error when using selenium-wire #85

Closed beibeiqi0916 closed 3 years ago

beibeiqi0916 commented 4 years ago

I am running the test suits by switching between selenium-wire and selenium, but looks like after the test cases triggered by selenium-wire completed, then the next url triggered by selenium is broken, say ERR_PROXY_CONNECTION_FAILED. I can see the last step in log for selenium-wire is 'INFO:seleniumwire.proxy.client:Destroying proxy', just guess that might be the reason. So are there any solutions I can check with?

wkeeling commented 4 years ago

Thanks for raising this. The driver instances should be independent of one another. Are you able to post the code that instantiates the selenium and selenium-wire webdrivers from your test?

beibeiqi0916 commented 4 years ago

Sure, I pass the wire as the parameter, so

            if wire:

driver = CustomWireChrome(executable_path=settings.CHROMEDRIVER_PATH, chrome_options=chrome_profile, desired_capabilities=dc) else: driver = CustomChrome(executable_path=settings.CHROMEDRIVER_PATH, chrome_options=chrome_profile, desired_capabilities=dc)

and I define CustomWireChrome is separate class

from seleniumwire import webdriver as wiredriver class CustomWireChrome(WebDriverExtensions, wiredriver.Chrome): pass

YourMan commented 4 years ago

I got the same error on Chrome. Did you find a solution, by any chance?

fantik11 commented 3 years ago

Same error in both Chrome and Firefox. Any solution?

wkeeling commented 3 years ago

I think the original issue is due to the two driver instances sharing the same desired_capabilities object (a dictionary). When the Selenium Wire instance CustomWireChrome is created, it modifies the desired capabilities by adding its internal proxy configuration to it. That means that the regular Selenium instance also receives this proxy configuration, but by the time that driver executes, the Selenium Wire proxy has disappeared and you see the error.

I'll look at fixing this in Selenium Wire so that the desired capabilities don't get "polluted" with this internal proxy config. In the meantime, it's probably a good idea to ensure that the two driver instances receive separate copies of the desired capabilities, rather than sharing the same instance. For example:

if wire:
    driver = CustomWireChrome(executable_path=settings.CHROMEDRIVER_PATH,
                          chrome_options=chrome_profile,
                          desired_capabilities=dict(dc))  # Use a copy
else:
    driver = CustomChrome(executable_path=settings.CHROMEDRIVER_PATH,
                          chrome_options=chrome_profile,
                          desired_capabilities=dc)
wkeeling commented 3 years ago

A copy of the desired capabilities is now made before it's used which eliminates side-effects caused by sharing. Available in v3.0.0 onwards.