Configuración de proxy en Selenium
Configura proxies rotativos en Selenium WebDriver para Chrome y Firefox.
Selenium WebDriver automatiza navegadores para pruebas y scraping. Configura SotaProxy en las ChromeOptions o el FirefoxProfile del navegador antes de lanzarlo.
Guía de configuración
Chrome con Python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--proxy-server=http://proxy.sotaproxy.com:10000')
driver = webdriver.Chrome(options=options)
# Handle authentication via extension or CDP
driver.get("https://httpbin.org/ip")
print(driver.page_source)
driver.quit()Chrome no admite la autenticación de proxy mediante argumentos de línea de comandos. Para proxies autenticados, usa una extensión de Chrome o el enfoque de Chrome DevTools Protocol (CDP) que aparece abajo.
Chrome con autenticación CDP (Python)
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import json, base64
PROXY_HOST = "proxy.sotaproxy.com"
PROXY_PORT = 10000
PROXY_USER = "YOUR_USERNAME"
PROXY_PASS = "YOUR_PASSWORD"
# Create auth extension
manifest = {
"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: {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"]
);
"""
import zipfile, os
ext_path = "/tmp/proxy_auth.zip"
with zipfile.ZipFile(ext_path, 'w') as zp:
zp.writestr("manifest.json", json.dumps(manifest))
zp.writestr("background.js", background_js)
options = Options()
options.add_extension(ext_path)
driver = webdriver.Chrome(options=options)
driver.get("https://httpbin.org/ip")
print(driver.page_source)Chrome requiere una extensión para manejar la autenticación de proxy. Esto crea una extensión mínima en memoria.
Firefox con Python
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.set_preference("network.proxy.type", 1)
options.set_preference("network.proxy.http", "proxy.sotaproxy.com")
options.set_preference("network.proxy.http_port", 10000)
options.set_preference("network.proxy.ssl", "proxy.sotaproxy.com")
options.set_preference("network.proxy.ssl_port", 10000)
driver = webdriver.Firefox(options=options)
# Firefox prompts for credentials - handle via alert
driver.get("https://httpbin.org/ip")Firefox admite la configuración de proxy de forma nativa mediante las preferencias del perfil. Maneja el popup de autenticación con driver.switch_to.alert.
Automatízalo con la API de SotaProxy
Todo lo anterior funciona sin abrir el panel. Con una clave API, Selenium puede obtener credenciales de proxy actualizadas, comprar nuevos proxies y renovar los que caducan - directamente desde el código.
import requests
API = "https://api.sotaproxy.com/api/v1"
H = {"Authorization": "Bearer sk_live_your_key"}
# Every active proxy on your account - no copy-pasting credentials
proxies = requests.get(f"{API}/proxies", headers=H).json()["proxies"]
p = proxies[0]
proxy_url = f"http://{p['login']}:{p['password']}@{p['ip']}:{p['portHttp']}"
# Buy more when you scale (price-check first with POST /quote)
requests.post(
f"{API}/orders",
headers={**H, "Idempotency-Key": "my-unique-order-id"},
json={"product": "ipv4", "countryId": 565, "periodId": "1m", "quantity": 5},
)The authentication gap in Selenium
Chrome and Firefox drivers accept a proxy address, but neither accepts a username and password through the standard capability. This is the single biggest source of confusion on this page:
Two ways out: run selenium-wire, which handles authenticated proxies natively, or generate a small Chrome extension that supplies the credentials. Both are shown below.
selenium-wire, or an extension you generate
selenium-wire is the shorter road and takes the login with suffixes exactly as we hand it out:
Python: selenium-wire with an authenticated proxy
from seleniumwire import webdriver
login = "login_c_US_s_3_ttl_1h"
opts = {
"proxy": {
"http": f"http://{login}:password@proxy.sotaproxy.com:10000",
"https": f"http://{login}:password@proxy.sotaproxy.com:10000",
"no_proxy": "localhost,127.0.0.1",
}
}
driver = webdriver.Chrome(seleniumwire_options=opts)
driver.get("https://api.ipify.org")- Change the session id and restart the driver to change address. There is no way to swap the proxy under a running Chrome session.
- Keep no_proxy for localhost, otherwise the driver tries to reach chromedriver through the proxy and the session never starts.
- selenium-wire intercepts traffic in Python, which costs throughput. For pure scraping, requests or Playwright will be several times faster.
- If you cannot add the dependency, build a two-file extension with a background script that answers the auth challenge and load it with add_extension.
Selenium specifics
Credentials in the --proxy-server flag are ignored
Exactly as in Puppeteer. Chrome takes host and port and nothing else, then challenges every request with 407.
A popup asks for the password
When Chrome cannot answer the proxy challenge it shows a native dialog that Selenium cannot click. That dialog is the symptom, not the cause.
The proxy applies to the whole browser
Per-tab addresses do not exist. One driver equals one address, so parallel identities mean parallel drivers.
Stale drivers hold sessions open
Always quit the driver. Abandoned Chrome processes keep their connection to the endpoint and quietly eat your concurrency.
Notas y consejos
- •Para pipelines de pruebas automatizadas, Playwright es más fácil de configurar con proxies autenticados que Selenium Chrome.
- •Las configuraciones de Selenium Grid pueden configurar el proxy a nivel de nodo - consulta la documentación de configuración de tu Grid.
FAQ
¿Por qué Chrome no admite la autenticación de proxy en Selenium?
Chrome eliminó el soporte de autenticación de proxy por línea de comandos por motivos de seguridad. Usa el método de extensión de arriba o cambia a Firefox/Playwright para una configuración de proxy autenticado más sencilla.
¿Puedo usar SotaProxy con Selenium Grid?
Sí. Configura el proxy en las capabilities del navegador al registrar los nodos, o pasa la configuración de proxy por prueba en tus desired capabilities.
Integraciones relacionadas
Obtén tus credenciales de proxy
Regístrate, recarga tu saldo y copia tu endpoint en Selenium. Tarda menos de 5 minutos.
Empezar