[Python] Selenium uses some other profile than the specified

Discussão em 'Python' iniciado por Stack, Junho 8, 2025.

  1. Stack

    Stack Membro Participativo

    I use selenium with this code to run google-chrome with my default profile:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from time import sleep
    from selenium.webdriver.common.keys import Keys
    base_url = r'C:\\Users\\administrator\Desktop'
    user_data = r'C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\User Data\\Default'
    options = webdriver.ChromeOptions()
    options.binary_location = r'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'
    options.add_argument(f'--user-data-dir={user_data}')
    options.add_argument('disable-infobars')
    options.add_experimental_option('excludeSwitches', ['enable-automation', 'test-type'])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=options)
    driver.get('https://www.google.com/ncr')
    search_box = driver.find_element(By.NAME, 'q')
    search_box.send_keys(f'how are you')
    search_box.send_keys(Keys.RETURN)
    sleep(1200)
    driver.quit()


    This code has some issues that I don't know why:

    1. When the code reaches sleep, I manually open chrome://version and I see that the chrome is using my user_data as profile, but the way selenium opens options.binary_location is different from when I type chrome in the Start Menu.

    I mean when I open chrome, I see no histories are there, but the output of chrome://version and things like binary location and profile data are exactly the same.


    1. I just see some differents: the selenium's chrome has some additional flags in the chrome://version, while opening manually the normal chrome has no flags.


    2. When I open google.com via selenium and google a keyword, it redirects me to https://www.google.com/sorry/index?continue=https://www.google.com/search and shows me captcha, while opening the normal chrome and doing exact the same does not show any captcha page (I'm not looking for any ways bypass captcha because I don't need that and it's better for me to do a way not seeing captcha instead of bypassing).

    Normal chrome flags:

    "C:\Program Files\Google\Chrome\Application\chrome.exe" --flag-switches-begin --flag-switches-end

    Selenium's chrome flags:

    "C:\Program Files\Google\Chrome\Application\chrome.exe" --allow-pre-commit-input --disable-background-networking --disable-backgrounding-occluded-windows --disable-client-side-phishing-detection --disable-default-apps --disable-hang-monitor --disable-infobars --disable-popup-blocking --disable-prompt-on-repost --disable-sync --enable-logging --log-level=0 --no-first-run --no-service-autorun --password-store=basic --remote-debugging-port=0 --use-mock-keychain --user-data-dir="C:\Users\Administrator\AppData\Local\Google\Chrome\User Data\Default" --flag-switches-begin --flag-switches-end --flag-switches-begin --flag-switches-end https://www.google.com/ncr

    In fact, I want selenium to run the normal chrome with no flags. I googled to run normal chrome, but it seems selenium removed executable_path since 4.0.

    Would you please help me how to run normal chrome, so that the instance is the actual chrome? I mean when I search chrome in Start Menu and click on it, it shouldn't run a new chrome.

    Edit1

    I added options.add_argument("--disable-blink-features=AutomationControlled") which made it a bit better and google does not show captcha, but still chrome is not the real instance.

    Continue reading...

Compartilhe esta Página