You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Selenium WebDriver代理配置方法及代码问题排查求助

Hey there! Let's tackle your Selenium proxy configuration issue step by step. From the code snippet you shared, it looks like you're trying to load a proxy from a text file but might be missing the actual setup logic for each browser driver. Here's how to fix this for Chrome, Firefox, and Edge, plus common pitfalls to check:

Selenium Proxy Configuration Guide for Major Browsers

First, make sure your proxy string in proxy.txt follows the correct format: IP:Port (e.g., 123.45.67.89:8080). If your proxy requires username/password authentication, we’ll cover that too.

1. Chrome Driver Proxy Setup

Modify your open_browser() function to configure Chrome with the proxy properly:

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

def open_browser():
    chromeUrl = "C:/Users/Drivers/chromedriver.exe"
    firefoxUrl = "C:/Users/Drivers/geckodriver.exe"
    edgeUrl = "C:/Users/Drivers/MicrosoftWebDriver.exe"

    print("Starting opening web browser")
    try:
        # Use with statement to auto-close the file
        with open('saved/proxy.txt','r') as f:
            proxy_ip = f.read().strip()  # Strip removes extra newlines/spaces
        
        if not proxy_ip:
            print("Proxy string is empty! Please check proxy.txt.")
            return
        
        PROXY = proxy_ip
        print(f"I have loaded a proxy: {PROXY}")

        # Configure Chrome options with proxy
        chrome_options = Options()
        chrome_options.add_argument(f'--proxy-server=http://{PROXY}')
        
        # Note: Modern Chrome blocks basic auth via command line. If your proxy needs auth,
        # you'll need to create a custom extension to handle credentials.
        
        driver = webdriver.Chrome(executable_path=chromeUrl, options=chrome_options)
        return driver
    except Exception as e:
        print(f"Error setting up proxy: {str(e)}")

2. Firefox Driver Proxy Setup

For Firefox, we use Proxy and FirefoxOptions to define proxy settings:

from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.proxy import Proxy, ProxyType

# Inside your try block (after loading proxy_ip):
proxy = Proxy()
proxy.proxy_type = ProxyType.MANUAL
proxy.http_proxy = PROXY
proxy.ssl_proxy = PROXY  # Required for HTTPS sites

firefox_options = Options()
firefox_options.proxy = proxy

# If your proxy requires authentication:
# firefox_options.set_preference("proxy.socks_username", "your_username")
# firefox_options.set_preference("proxy.socks_password", "your_password")

driver = webdriver.Firefox(executable_path=firefoxUrl, options=firefox_options)

3. Edge Driver Proxy Setup

Since Edge is Chromium-based, its proxy setup mirrors Chrome:

from selenium.webdriver.edge.options import Options

# Inside your try block (after loading proxy_ip):
edge_options = Options()
edge_options.add_argument(f'--proxy-server=http://{PROXY}')

# For proxy auth, same as Chrome—use a custom extension
driver = webdriver.Edge(executable_path=edgeUrl, options=edge_options)

Common Issues to Troubleshoot

  • Proxy Format Errors: Extra spaces, newlines, or invalid IP:Port syntax will break the setup. Using strip() on the loaded proxy string fixes most of these.
  • Driver-Browser Mismatch: Ensure your driver version (e.g., ChromeDriver) exactly matches your installed browser version. Mismatches often cause silent failures or crashes.
  • Incomplete Code: Your original snippet had if len(proxy_ip) &... which is a syntax error. Use if not proxy_ip to check for empty proxy strings instead.
  • Proxy Authentication: As noted above, Chrome/Edge no longer support basic auth via command line—you’ll need a custom extension to pass credentials. Firefox allows direct preference settings for auth.

If you still run into specific errors, share the full error message and your proxy’s exact format, and we can narrow it down further!

内容的提问来源于stack exchange,提问作者Kamil Septio Trojnar

火山引擎 最新活动