When attempting to automate tasks using Selenium and Chrome, certain websites may block requests based on the detection of a Selenium-driven browser. One common method of detection involves exposing a DOM variable called navigator.webdriver, which returns true if the browser is controlled by Selenium.
To circumvent this detection, consider the following approaches:
from selenium import webdriver options = webdriver.ChromeOptions() options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option("useAutomationExtension", False) driver = webdriver.Chrome(options=options, executable_path=r"path/to/chromedriver.exe")
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
driver.execute_cdp_cmd("Network.setUserAgentOverride", {"userAgent": "new_user_agent"})
options.add_argument("--disable-blink-features=AutomationControlled")
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=r"path/to/chromedriver.exe") driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})") driver.execute_cdp_cmd("Network.setUserAgentOverride", {"userAgent": "new_user_agent"}) driver.get("https://www.example.com")
The NavigatorAutomationInformation interface includes the navigator.webdriver flag, which returns true when the browser is controlled by WebDriver. However, altering these parameters may cause navigation issues or detection if used improperly.
Recent versions of Selenium offer additional features for WebDriver control, including the execute_cdp_cmd() command for executing DevTools commands. Utilizing this command provides a convenient way to modify the navigator.webdriver flag and prevent Selenium 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!