You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Selenium+Chromium无法访问映射至localhost的域名怎么办?

解决Chromium+Selenium忽略/etc/hosts映射的问题

你遇到的问题是Chromium在Selenium自动化模式下会绕过系统/etc/hosts配置,导致example.com无法解析到127.0.0.1。以下是几个可行的解决办法:

1. 直接强制DNS映射(最可靠)

在ChromeOptions中添加--host-resolver-rules参数,强制让Chromium内部将example.com解析到本地IP,完全不依赖系统hosts配置:

chrome_options.add_argument("--host-resolver-rules='MAP example.com 127.0.0.1'")

2. 启用自定义用户数据目录

取消你代码中注释掉的用户数据目录参数。Chromium在默认临时配置环境下可能不会加载系统hosts,使用自定义目录可让它读取系统级配置:

chrome_options.add_argument("--user-data-dir=/tmp/chrome-debug-fresh")

注意:首次启动会自动创建该目录,需确保运行用户拥有读写权限。

3. 补充安全限制禁用参数

如果本地服务器使用HTTP或自签HTTPS证书,添加以下参数避免被Chromium安全机制拦截:

chrome_options.add_argument("--ignore-certificate-errors")
chrome_options.add_argument("--allow-insecure-localhost")

修改后的完整代码示例

import os
import time

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

# Get window size with 720p as default.
window_size = os.environ.get("WINDOW_SIZE", "1280,720")

# Configure Chrome options
chrome_options = Options()
chrome_options.binary_location = os.environ["CHROME_PATH"]

# Essential for headless automation
chrome_options.add_argument(f"--window-size={window_size}")
chrome_options.add_argument("--headless=new")  # 新版无头模式更接近正常浏览器行为
chrome_options.add_argument("--no-sandbox")  # Required in Docker containers
chrome_options.add_argument("--remote-debugging-port=9222")  # Enable automation protocol
chrome_options.add_argument("--user-data-dir=/tmp/chrome-debug-fresh")  # 启用自定义用户目录

# 强制DNS映射
chrome_options.add_argument("--host-resolver-rules='MAP example.com 127.0.0.1'")

# 禁用安全限制
chrome_options.add_argument("--ignore-certificate-errors")
chrome_options.add_argument("--allow-insecure-localhost")

# Startup time optimizations
chrome_options.add_argument("--no-first-run")  # Skip first-run setup
chrome_options.add_argument("--no-default-browser-check")  # Skip default browser prompt
chrome_options.add_argument("--disable-extensions")  # No extension loading
chrome_options.add_argument("--disable-component-update")  # Skip component updates
chrome_options.add_argument("--disable-default-apps")  # Skip default apps loading
chrome_options.add_argument("--disable-sync")  # Skip sync services

# Performance optimizations
chrome_options.add_argument("--disable-background-networking")  # Reduce network overhead
chrome_options.add_argument("--disable-background-timer-throttling")  # Better resource control
chrome_options.add_argument("--disable-backgrounding-occluded-windows")  # Resource management
chrome_options.add_argument("--disable-renderer-backgrounding")  # Keep renderers active

# Docker-specific optimizations
chrome_options.add_argument("--disable-gpu")  # GPU not available in container
chrome_options.add_argument("--disable-features=VizDisplayCompositor")  # Compositor optimization
chrome_options.add_argument("--disable-web-security")  # Bypass CORS for local development
chrome_options.add_argument("--disable-dev-shm-usage")  # Use /tmp instead of /dev/shm

service = Service(executable_path=os.environ["CHROMEDRIVER_PATH"])

max_retries = 5
retry_delay = 2

for attempt in range(max_retries):
    try:
        driver = webdriver.Chrome(service=service, options=chrome_options)
        break
    except Exception:
        if attempt < max_retries - 1:
            print(f"Chrome connection attempt {attempt + 1} failed, retrying in {retry_delay}s...")
            time.sleep(retry_delay)
        else:
            print(f"Failed to connect to Chrome after {max_retries} attempts")
            raise

driver.get("http://example.com")
# 可选:添加验证代码确认是否访问到本地服务器
print(driver.current_url)
print(driver.page_source[:500])  # 打印页面前500字符
driver.quit()

额外说明

  • 推荐使用--headless=new替代旧版--headless,新版无头模式行为更接近正常浏览器,兼容性更好。
  • 若在Docker中运行,需确保/tmp/chrome-debug-fresh目录有读写权限,或挂载到宿主机目录。

内容的提问来源于stack exchange,提问作者Xodarap

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 05:40:05