Selenium Proxy: Complete Guide
Selenium WebDriver is a popular browser automation tool. Setting up proxies in Selenium requires a special approach.
Chrome + Selenium
Basic Setup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--proxy-server=http://proxy.example.com:8080')
driver = webdriver.Chrome(options=chrome_options)
driver.get('https://httpbin.org/ip')
With Authentication (via extension)
import zipfile
def create_proxy_extension(proxy_host, proxy_port, proxy_user, proxy_pass):
manifest_json = '''
{
"version": "1.0.0",
"manifest_version": 2,
"name": "Proxy Auth",
"permissions": ["proxy", "webRequest", "webRequestBlocking", "<all_urls>"],
"background": {"scripts": ["background.js"]}
}
'''
background_js = f'''
var config = {{
mode: "fixed_servers",
rules: {{
singleProxy: {{
scheme: "http",
host: "{proxy_host}",
port: parseInt({proxy_port})
}}
}}
}};
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']
);
'''
with zipfile.ZipFile('proxy_auth.zip', 'w') as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", background_js)
return 'proxy_auth.zip'
# Usage
ext_path = create_proxy_extension('proxy.example.com', 8080, 'user', 'pass')
chrome_options = Options()
chrome_options.add_extension(ext_path)
driver = webdriver.Chrome(options=chrome_options)
Firefox + Selenium
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", "proxy.example.com")
profile.set_preference("network.proxy.http_port", 8080)
profile.set_preference("network.proxy.ssl", "proxy.example.com")
profile.set_preference("network.proxy.ssl_port", 8080)
driver = webdriver.Firefox(firefox_profile=profile)
SOCKS5 Proxy
chrome_options = Options()
chrome_options.add_argument('--proxy-server=socks5://proxy.example.com:1080')
Proxy Rotation
import random
proxies = [
'http://proxy1.example.com:8080',
'http://proxy2.example.com:8080',
'http://proxy3.example.com:8080',
]
def get_driver_with_proxy():
proxy = random.choice(proxies)
chrome_options = Options()
chrome_options.add_argument(f'--proxy-server={proxy}')
return webdriver.Chrome(options=chrome_options)
Detection Bypass
chrome_options = Options()
chrome_options.add_argument('--proxy-server=http://proxy:8080')
chrome_options.add_argument('--disable-blink-features=AutomationControlled')
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
chrome_options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=chrome_options)
driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'
})
Verification
driver.get('https://httpbin.org/ip')
print(driver.page_source)
Tips
- Use headless mode carefully — many sites detect it
- Add random delays between actions
- Rotate User-Agent along with proxy
- Use residential proxies for difficult sites
Обновлено: 09.01.2026
Назад к категории