Home >Java >javaTutorial >How Can I Prevent Selenium Detection by Modifying the Navigator.webdriver Flag?

How Can I Prevent Selenium Detection by Modifying the Navigator.webdriver Flag?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-24 06:35:25647browse

How Can I Prevent Selenium Detection by Modifying the Navigator.webdriver Flag?

Modifying Navigator.webdriver Flag to Prevent Selenium Detection

Selenium detection poses a significant challenge when automating mundane website functionalities through Selenium and Chrome. Some websites actively check for Selenium-driven browsers, preventing certain requests. Often, they rely on exposed DOM variables, like navigator.webdriver, to detect such instances.

Prevention Approach

To thwart this detection mechanism, consider adopting the following preventive measures:

Add Command-Line Flags

Modify your Selenium script to inject the specific arguments into the Chrome instance. This includes:

  • Disabling the "AutomationControlled" flag:

    from selenium import webdriver
    
    options = webdriver.ChromeOptions()
    options.add_argument('--disable-blink-features=AutomationControlled')
    driver = webdriver.Chrome(options=options, executable_path=path_to_driver)
  • Setting a custom user agent:

    driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'Your_Custom_User_Agent'})

In-script Modifications

  • Set navigator.webdriver to undefined:

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

    options.add_experimental_option("excludeSwitches", ["enable-automation"])
  • Disable automation extension:

    options.add_experimental_option('useAutomationExtension', False)

Sample Code

Combining these measures, here's a comprehensive code snippet:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)

driver = webdriver.Chrome(options=options, executable_path=path_to_driver)
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'Your_Custom_User_Agent'})
print(driver.execute_script("return navigator.userAgent;"))
driver.get('https://www.httpbin.org/headers')

Caveats

Be cautious as these modifications may interfere with navigation and potentially lead to detection.

The above is the detailed content of How Can I Prevent Selenium Detection by Modifying the Navigator.webdriver Flag?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn