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

ERR_PROXY_CONNECTION_FAILED when script closes in seleniumwire and using TOR #372

Closed shreesh1 closed 3 years ago

shreesh1 commented 3 years ago

Maybe not a bug but a difference in selenium and selenium-wire

Brief:

Let's say we a browse a site through selenium over TOR and the script as it gets completed closes itself the TOR proxy still runs in the browser, but as the same case running selenium-wire over TOR proxy as the script gets completed and it shuts itself the TOR proxy in the browser closes down and you can't interact further as PROXY fails.

How to reproduce the issue:

from seleniumwire import webdriver
#from selenium import webdriver

import time
def wire():
    sw_options = {  # Ensure this is set to False
        'proxy': {
            'http': 'socks5://127.0.0.1:9050',
            'https': 'socks5://127.0.0.1:9050'
        }
    }
    chrome_options = webdriver.ChromeOptions()
#chrome_options.add_argument('--headless')

    browser = webdriver.Chrome(executable_path="./drivers/chromedriver",options=chrome_options,seleniumwire_options=sw_options)
    return browser

def notwire():
    PROXY = "socks5://127.0.0.1:9050"
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--proxy-server=%s' % PROXY)
    browser = webdriver.Chrome(executable_path="./drivers/chromedriver",options=chrome_options)
    return browser

if __name__ == "__main__":
    browser = wire()
    browser.get("https://dnsleaktest.com")
    browser.find_element_by_xpath("//input[@value='Standard test']").click()

Just switch between selenium-wire and selenium imports and wire and notwire function respectively and the test will run smoothly in case of selenium but in case of selenium-wire the test won't run, just open up the "CONSOLE" and there would be ERR_PROXY_CONNECTION_FAILED as the TOR proxy have stopped

wkeeling commented 3 years ago

Thanks for raising this.

Just to clarify, are you saying that the line browser.get("https://dnsleaktest.com") does not work when you use Selenium Wire?

shreesh1 commented 3 years ago

Nope, that works fine,

Let's say you just execute this code.

from seleniumwire import webdriver
sw_options = {
        'proxy': {
            'http': 'socks5://127.0.0.1:9050',
            'https': 'socks5://127.0.0.1:9050'
        }
}
browser = webdriver.Chrome(executable_path="./drivers/chromedriver",seleniumwire_options=sw_options)
browser.get("https://www.google.com")

The page *www.google.com* is loaded fine but you can't search anything through it or let's say surf the internet.

Cause as the work for the script is already done, it had already terminated itself but browser is still there as we didn't add browser.quit() and now if you want to search anything in there you can't, cause the TOR proxy for the browser has also shutdown with the script.

Where else in the case of selenium even after script has terminated, the TOR proxy still remains in the browser and we can search through through google or let's say you can even surf the internet after terminating the script as the proxy remains there in the browser.

wkeeling commented 3 years ago

Thanks for clarifying.

Yes once the script ends, the proxy that Selenium Wire spins up in the background to capture requests will go away, which means the browser won't be usable because it will still be configured to use that proxy.

This does catch people out from time to time. It may be that we can add some config option which when set, will keep the proxy thread running. I'll give it some thought.

shreesh1 commented 3 years ago

Sure, Thanks a lot!

Though, for me the workaround is I just put the script to sleep through time.sleep(secs) so that script keeps running and proxy still remains there.