2026, Jan 05 03:00

How to Solve Microsoft Edge Selenium WebDriver 'session not created' Error Using a Unique --user-data-dir

Learn why Microsoft Edge Selenium WebDriver fails with 'session not created' and fix it by launching with a unique --user-data-dir profile for reliable runs.

When launching Microsoft Edge via Selenium for a simple image parsing task, it’s easy to run into a startup failure with the message: “session not created: probably user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir.” This guide shows a minimal reproducible setup, explains why it happens, and demonstrates a reliable fix.

Problem

The browser is initialized with custom options, but without a dedicated profile directory. A run may fail with the mentioned error.

def bootstrap_driver():
    opts = EdgeOptions()
    opts.add_argument('--start-maximized')
    opts.add_argument('--no-sandbox')
    opts.add_argument('--disable-dev-shm-usage')
    opts.add_argument('--disable-blink-features=AutomationControlled')
    opts.add_experimental_option('excludeSwitches', ['enable-automation'])
    opts.add_experimental_option('useAutomationExtension', False)
    opts.add_argument('--disable-gpu')
    opts.add_argument('--disable-software-rasterizer')
    try:
        manager = EdgeChromiumDriverManager()
        bin_path = manager.install()
        srv = EdgeService(bin_path)
        agent = webdriver.Edge(service=srv, options=opts)
        logging.info(f"Edge driver installed: {bin_path}")
    except Exception as err:
        logging.error(f"Error driver installed: {str(err)}")
        raise err
    agent.execute_script(
        "Object.defineProperty(navigator, 'webdriver', {get: () => undefined})"
    )
    return agent

What’s going on and why it breaks

The startup error indicates that the browser’s user data directory is already in use. In practice, this happens when the same profile path is locked by another Edge instance. Ensuring there’s no leftover Edge process can help as a sanity check. The robust route is to launch WebDriver with a unique profile path so each run has an isolated user data directory.

Solution: set a unique --user-data-dir

Provide a dedicated temporary directory for the Edge profile. This removes contention and makes sessions predictable.

import tempfile
import os
def init_edge_driver():
    edge_opts = EdgeOptions()
    edge_opts.add_argument('--start-maximized')
    edge_opts.add_argument('--no-sandbox')
    edge_opts.add_argument('--disable-dev-shm-usage')
    edge_opts.add_argument('--disable-blink-features=AutomationControlled')
    edge_opts.add_experimental_option('excludeSwitches', ['enable-automation'])
    edge_opts.add_experimental_option('useAutomationExtension', False)
    edge_opts.add_argument('--disable-gpu')
    edge_opts.add_argument('--disable-software-rasterizer')
    # Use a unique temp directory for user data
    data_home = os.path.join(tempfile.gettempdir(), 'edge_automation_profile')
    edge_opts.add_argument(f'--user-data-dir={data_home}')
    try:
        drv_mgr = EdgeChromiumDriverManager()
        drv_bin = drv_mgr.install()
        svc_obj = EdgeService(drv_bin)
        client = webdriver.Edge(service=svc_obj, options=edge_opts)
        logging.info(f"Edge driver installed: {drv_bin}")
    except Exception as ex:
        logging.error(f"Error installing driver: {str(ex)}")
        raise ex
    client.execute_script(
        "Object.defineProperty(navigator, 'webdriver', {get: () => undefined})"
    )
    return client

If you still experience contention, make the directory name unique per process to avoid any overlap across concurrent runs.

data_home = os.path.join(
    tempfile.gettempdir(),
    f"edge_automation_profile_{os.getpid()}"
)

Why this matters

Automation depends on predictable, isolated environments. A unique profile directory eliminates resource conflicts between Edge instances and keeps your startup sequence deterministic. It also aligns with the error’s explicit guidance to provide a unique --user-data-dir.

Takeaways

Initialize Edge with a dedicated --user-data-dir to prevent the “session not created” failure caused by an in-use profile path. If collisions persist, incorporate the process ID into the directory name for guaranteed uniqueness. And as a quick sanity check before a run, make sure Edge isn’t already open or lingering in the process list.