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
- 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(";)
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!
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
- 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")
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!
Share asked Mar 11 at 2:51 elctl02elctl02 1 1- 1 "to automate browser testing" - I have to assume that's not really what you're doing, because you could just turn off the automation detection to do that. Since the site you're trying to automate is clearly strongly resisting that automation, have you considered any APIs etc. that would avoid going through the front door? – Grismar Commented Mar 11 at 3:38
1 Answer
Reset to default -1You are violating Google terms of use! - they will not allow you to use automation on their websites or services repeatedly without having to pay an API license. Hence why they have anti-bot captchas to protect their systems.
Try picking another website to automate that will allow you to do such things!