Issue connecting to socks5 proxy with chrome web driver selenium python

133 views Asked by At

I am trying to use chrome web driver with selenium with a socks5 proxy. Since you can't use authentication proxys with chromedriver, only ones that don't require auth, I have worked around this by creating an extension and connecting to the proxy with the extension. It is opening, but it will not connect to any websites. I will include my code, along with the proxy server so you can try it and see what happens.

import os
import zipfile
from selenium import webdriver

PROXY_HOST = '31.43.185.85'  # rotating proxy or host
PROXY_PORT = 57425  # port
PROXY_USER = '443808b7f7b8'  # username
PROXY_PASS = '6526ade8d11a'  # password

manifest_json = f'''
{{
    "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": "22.0.0"
}}
'''

background_js = f'''
var config = {{
    mode: "fixed_servers",
    rules: {{
        singleProxy: {{
            scheme: "http",
            host: "{PROXY_HOST}",
            port: parseInt({PROXY_PORT})
        }},
        bypassList: ["localhost"]
    }}
}};

chrome.proxy.settings.set({{value: config, scope: "regular"}}, function() {{}});

function callbackFn(details) {{
    return {{
        authCredentials: {{
            username: "{PROXY_USER}",
            password: "{PROXY_PASS}"
        }}
    }};
}}

chrome.webRequest.onAuthRequired.addListener(
    callbackFn,
    {{urls: ["<all_urls>"]}},
    ['blocking']
);
'''

def create_proxy_extension():
    with zipfile.ZipFile('proxy_extension.zip', 'w') as zp:
        zp.writestr("manifest.json", manifest_json)
        zp.writestr("background.js", background_js)

def get_chromedriver(use_proxy=False, user_agent=None):
    path = os.path.dirname(os.path.abspath(__file__))
    if use_proxy:
        create_proxy_extension()

    chrome_options = webdriver.ChromeOptions()
    if use_proxy:
        chrome_options.add_extension(os.path.join(path, 'proxy_extension.zip'))
    if user_agent:
        chrome_options.add_argument(f'--user-agent={user_agent}')

    # Specify the path to your chromedriver.exe here
    chromedriver_path = 'C:/Users/User/AppData/Local/Programs/Python/Python311/Scripts/chromedriver-win64/chromedriver.exe'
    chrome_options.add_argument(f'--chromedriver-executable={chromedriver_path}')

    driver = webdriver.Chrome(options=chrome_options)
    return driver

def main():
    driver = get_chromedriver(use_proxy=True)
    driver.get('https://httpbin.org/ip')

if __name__ == '__main__':
    main()

I've tried going to different websites, I've tried different proxies, same issue.

0

There are 0 answers