[Python] Selenium Automation in Firefox (Python) Blocked by Captchas & "Browser is Being...

Discussão em 'Python' iniciado por Stack, Março 11, 2025.

  1. Stack

    Stack Membro Participativo

    Good day,

    I'm using Selenium with Firefox in Python to automate browser testing, but I keep encountering captchas that prevent my script from proceeding. Additionally, I receive an error stating that "the browser is being automated" (Reason: Marionette).

    My intended automation flow:

    • Open Firefox
    • Go to www.google.com
    • Search for "YouTube"
    • Click on the YouTube link
    • Search for a video
    • Play the video

    However, captchas appear during the process, and I suspect it's because Firefox detects that it's automated.

    In Chrome, I was able to reduce automation detection using the following options:

    options = webdriver.ChromeOptions()
    options.add_argument("--disable-blink-features=AutomationControlled")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option("useAutomationExtension", False)
    user_data_dir = os.path.join(os.getcwd(), "chrome_user_data")
    options.add_argument(f"user-data-dir={user_data_dir}")
    service = webdriver.chrome.service.Service(ChromeDriverManager().install())
    driver = webdriver.Chrome(service=service, options=options)


    To achieve something similar in Firefox, I've already tried the following, but they didn’t seem to work:

    options = webdriver.FirefoxOptions()
    options.set_preference("dom.webdriver.enabled", False) # Disable WebDriver flag
    options.set_preference("dom.webnotifications.enabled", False) # Disable notifications
    options.set_preference("media.peerconnection.enabled", False) # Disable WebRTC (can expose bot)
    options.set_preference("network.http.referer.spoofSource", True) # Spoof referer
    options.set_preference("privacy.trackingprotection.enabled", False) # Avoid bot flags
    options.set_preference("useAutomationExtension", False)
    options.set_preference("general.platform.override", "Win32") # Mimic Windows OS
    options.set_preference("general.useragent.override", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36") # Fake User-Agent

    #Start WebDriver
    service = Service(GeckoDriverManager().install())
    driver = webdriver.Firefox(service=service, options=options)

    driver.get("https://www.google.com")


    I've also tried doing the following:

    profile_path = "/path/to/firefox/profile" options.add_argument(f"-profile {profile_path}")

    driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")

    driver.execute_script("window.navigator.webdriver = false;")

    Despite these attempts, I'm still encountering captchas and automation detection. Is there an effective way to disable automation detection in Firefox or configure Marionette to prevent captchas while using Selenium in Python?

    Any guidance or workarounds would be greatly appreciated.

    Thank you!

    Continue reading...

Compartilhe esta Página