0

I am a newbie and trying to write a picture parser, starting to learn parsing have a code that gives an error, how can I fix it

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

And i get error:

Message: session not created: probably user data directory is already in us
e, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir
New contributor
Даниил Губанов is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
2
  • did you close your browser ? Maybe you have Edge open somewhere else. You may also check processes and kill all running Edges
    – furas
    Commented 16 hours ago
  • Yep, i closed edge and check processes It's not there Commented 16 hours ago

1 Answer 1

0

Add a Unique --user-data-dir Argument. Modify your setup_browser() function to include a unique temp directory:

# new imports
import tempfile
import os

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

Note: If you still experience any issue, please attempt to use the code below instead of "user_data_dir = os.path.join(tempfile.gettempdir(), "edge_automation_profile")":

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.