I try to use a proxy server with selenium, on average I manage to properly connect and open the page once every few minutes. When I fail, an error pops up "net::ERR_CONNECTION_RESET" My code:
import undetected_chromedriver as webdriver
...
class ProxyExtension:
manifest_json = """
{
"version": "1.0.0",
"manifest_version": 2,
"name": "Chrome Proxy",
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"<all_urls>",
"webRequest",
"webRequestBlocking"
],
"background": {"scripts": ["background.js"]},
"minimum_chrome_version": "76.0.0"
}
"""
background_js = """
var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "http",
host: "%s",
port: %d
},
bypassList: ["localhost"]
}
};
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
function callbackFn(details) {
return {
authCredentials: {
username: "%s",
password: "%s"
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{ urls: ["<all_urls>"] },
['blocking']
);
"""
def __init__(self, host, port, user, password):
self._dir = os.path.normpath(tempfile.mkdtemp())
manifest_file = os.path.join(self._dir, "manifest.json")
with open(manifest_file, mode="w") as f:
f.write(self.manifest_json)
background_js = self.background_js % (host, port, user, password)
background_file = os.path.join(self._dir, "background.js")
with open(background_file, mode="w") as f:
f.write(background_js)
@property
def directory(self):
return self._dir
def __del__(self):
shutil.rmtree(self._dir)
options = [("123.123.123", 123, "xxx", "xxx"),
("", , "", ""),
(),
(),
(),
()]
if __name__ == "__main__":
number_of_proxy = int(input('Input number of proxy: '))
proxy = options[number_of_proxy-1]
print(f'Bot start with proxy: {proxy}')
proxy_extension = ProxyExtension(*proxy)
options = webdriver.ChromeOptions()
options.add_argument(f"--load-extension={proxy_extension.directory}")
options.add_argument('--start-maximized')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=options)
driver.get(URL_CONF)
Could it be the fault of the proxy provider? I found some free proxies on the internet with which I have no such problem