Selenium Integration

Selenium Proxy SetupWebDriver automation with mobile IPs

Selenium is the industry-standard browser automation framework. Configure SOCKS5 and HTTP proxies with Chrome, Firefox, and Edge for reliable web scraping.

Selenium Features

  • Chrome, Firefox, Edge support
  • Python, Java, JavaScript bindings
  • WebDriver proxy options
  • Selenium Wire for advanced proxy

Configuration Examples

1. Python + Chrome + SOCKS5

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()

# SOCKS5 proxy configuration
options.add_argument('--proxy-server=socks5://proxy.proxies.sx:10001')

# Anti-detection measures
options.add_argument('--disable-blink-features=AutomationControlled')
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_experimental_option('useAutomationExtension', False)

# Headless mode
options.add_argument('--headless=new')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome(options=options)

# Execute CDP command to modify navigator.webdriver
driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
    'source': '''
        Object.defineProperty(navigator, 'webdriver', {get: () => undefined})
    '''
})

driver.get('https://httpbin.org/ip')
print(driver.page_source)
driver.quit()

2. Selenium Wire (Proxy Authentication)

Standard Selenium doesn't support SOCKS5 auth. Use Selenium Wire for full authentication.

# pip install selenium-wire
from seleniumwire import webdriver

proxy_options = {
    'proxy': {
        'http': 'socks5://your_username:your_password@proxy.proxies.sx:10001',
        'https': 'socks5://your_username:your_password@proxy.proxies.sx:10001',
        'no_proxy': 'localhost,127.0.0.1'
    }
}

options = webdriver.ChromeOptions()
options.add_argument('--headless=new')
options.add_argument('--disable-blink-features=AutomationControlled')

driver = webdriver.Chrome(
    options=options,
    seleniumwire_options=proxy_options
)

driver.get('https://httpbin.org/ip')
print(driver.page_source)
driver.quit()

3. Firefox with Proxy

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()

# Firefox proxy preferences
options.set_preference('network.proxy.type', 1)
options.set_preference('network.proxy.socks', 'proxy.proxies.sx')
options.set_preference('network.proxy.socks_port', 10001)
options.set_preference('network.proxy.socks_version', 5)
options.set_preference('network.proxy.socks_remote_dns', True)

# For HTTP proxy instead:
# options.set_preference('network.proxy.http', 'proxy.proxies.sx')
# options.set_preference('network.proxy.http_port', 10001)
# options.set_preference('network.proxy.ssl', 'proxy.proxies.sx')
# options.set_preference('network.proxy.ssl_port', 10001)

options.add_argument('--headless')

driver = webdriver.Firefox(options=options)
driver.get('https://httpbin.org/ip')
print(driver.page_source)
driver.quit()

4. Chrome Extension Method (Auth)

import zipfile
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def create_proxy_extension(proxy_host, proxy_port, proxy_user, proxy_pass):
    """Create Chrome extension for proxy authentication"""
    manifest_json = """
    {
        "version": "1.0.0",
        "manifest_version": 2,
        "name": "Proxy Auth Extension",
        "permissions": ["proxy", "tabs", "unlimitedStorage", "storage", "webRequest", "webRequestBlocking", "<all_urls>"],
        "background": {"scripts": ["background.js"]},
        "minimum_chrome_version": "22.0.0"
    }
    """

    background_js = f"""
    var config = {{
        mode: "fixed_servers",
        rules: {{
            singleProxy: {{
                scheme: "socks5",
                host: "{proxy_host}",
                port: parseInt({proxy_port})
            }},
            bypassList: ["localhost"]
        }}
    }};

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

    chrome.webRequest.onAuthRequired.addListener(
        function(details) {{
            return {{
                authCredentials: {{
                    username: "{proxy_user}",
                    password: "{proxy_pass}"
                }}
            }};
        }},
        {{urls: ["<all_urls>"]}},
        ['blocking']
    );
    """

    extension_path = 'proxy_auth_extension.zip'
    with zipfile.ZipFile(extension_path, 'w') as zp:
        zp.writestr("manifest.json", manifest_json)
        zp.writestr("background.js", background_js)

    return extension_path

# Create and use extension
ext_path = create_proxy_extension(
    'proxy.proxies.sx', 10001, 'your_username', 'your_password'
)

options = Options()
options.add_extension(ext_path)

driver = webdriver.Chrome(options=options)
driver.get('https://httpbin.org/ip')
print(driver.page_source)
driver.quit()

5. Java + Chrome

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class ProxyExample {
    public static void main(String[] args) {
        ChromeOptions options = new ChromeOptions();

        // SOCKS5 proxy
        options.addArguments("--proxy-server=socks5://proxy.proxies.sx:10001");

        // Anti-detection
        options.addArguments("--disable-blink-features=AutomationControlled");
        options.setExperimentalOption("excludeSwitches",
            new String[]{"enable-automation"});

        // Headless
        options.addArguments("--headless=new");

        WebDriver driver = new ChromeDriver(options);

        driver.get("https://httpbin.org/ip");
        System.out.println(driver.getPageSource());

        driver.quit();
    }
}

6. JavaScript/Node.js

const { Builder } = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');

(async () => {
  const options = new chrome.Options();

  options.addArguments('--proxy-server=socks5://proxy.proxies.sx:10001');
  options.addArguments('--disable-blink-features=AutomationControlled');
  options.addArguments('--headless=new');

  const driver = await new Builder()
    .forBrowser('chrome')
    .setChromeOptions(options)
    .build();

  try {
    await driver.get('https://httpbin.org/ip');
    const pageSource = await driver.getPageSource();
    console.log(pageSource);
  } finally {
    await driver.quit();
  }
})();
Note: Standard Selenium doesn't support SOCKS5 authentication natively. Use Selenium Wire or the Chrome Extension method for authenticated proxies.

Selenium Proxy Best Practices

Use Selenium Wire

For proxy authentication, Selenium Wire is the cleanest solution. It supports SOCKS5 auth without extension workarounds.

Add Anti-Detection

Remove automation indicators with CDP commands and Chrome flags. Combine with mobile proxies for maximum stealth.

Use Headless New Mode

The new headless mode (--headless=new) has better compatibility and is harder to detect than the old mode.

Always Quit Driver

Use try/finally to ensure driver.quit() is called. Leaked browser processes consume resources and can cause issues.

Start Automating with Selenium

Get mobile proxies for reliable browser automation.