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
Release: 2024-12-24 06:35:25
Original
642 people have browsed it

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)
    Copy after login
  • Setting a custom user agent:

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

In-script Modifications

  • Set navigator.webdriver to undefined:

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

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

    options.add_experimental_option('useAutomationExtension', False)
    Copy after login

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')
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template