In /etc/hosts I map 127.0.0.1 example.com, because I want the user to be able to navigate to example.com like it's real but it's actually going to my local dev server. However, it seems like Chrome security settings prevent this; curl example.com works, as does using selenium with Firefox, but chromium fails.
Is there a way to do this?
Here is the code I'm using:
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") # Run without GUI
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")
# 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")
127.0.0.1 example.com. I'm not getting an error, it's just thatdriver.page_sourceis<html><head></head><body></body></html>