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.
To thwart this detection mechanism, consider adopting the following preventive measures:
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'})
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)
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')
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!