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-23 19:22:17
Original
241 people have browsed it

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

Preventing Selenium Detection: Modifying the navigator.webdriver Flag

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:

Excluding Automation Switches

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

Modifying navigator.webdriver

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

Changing User-Agent

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

Disabling Blink Features

options.add_argument("--disable-blink-features=AutomationControlled")
Copy after login

Comprehensive Code Example

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

Historical Considerations

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.

Updates

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!

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