Selenium, a popular web automation framework, faces challenges when websites implement detection mechanisms that flag Selenium-driven browsers. This article aims to address such detection issues by providing effective methods to modify the navigator.webdriver flag, thereby preventing Selenium's detection.
Add Chrome Arguments:
options = webdriver.ChromeOptions() options.add_argument('--disable-blink-features=AutomationControlled') driver = webdriver.Chrome(options=options)
Execute CDP Command:
driver.execute_cdp_cmd('Network.setUserAgentOverride', {'userAgent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.53 Safari/537.36'})
Modify navigator.webdriver Property:
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 the above methods into a single code block:
from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument('--disable-blink-features=AutomationControlled') options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) driver = webdriver.Chrome(options=options) driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})") driver.execute_cdp_cmd('Network.setUserAgentOverride', {'userAgent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.53 Safari/537.36'}) driver.get('https://www.website.com/')
The navigator.webdriver flag is defined by the W3C as a way to indicate that a user agent is under remote control.
Tweaking these parameters may interfere with browser navigation and make the WebDriver instance detectable.
By following these methods, you can effectively modify the navigator.webdriver flag and prevent websites from detecting your Selenium-driven browser. However, it's important to handle these changes with caution to avoid detection.
The above is the detailed content of How Can I Prevent Selenium Webdriver from Being Detected by Websites?. For more information, please follow other related articles on the PHP Chinese website!