0

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")
2
  • 1
    chromedriver will also use localhost for wire protocol. (are there any entries in hosts file for localhost? you may also need to restart after any changes there) Can you include the exception/error you receive? (and setting --remote-debugging-port is for attaching to existing IDE launched sessions... if you are doing that make sure the port matches what your IDE is using to launch the browser) Commented Oct 10 at 16:47
  • @browsermator yes! hostfile has 127.0.0.1 example.com. I'm not getting an error, it's just that driver.page_source is <html><head></head><body></body></html> Commented Oct 11 at 3:47

1 Answer 1

0

I was able to solve this by supporting SSL.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.