首先更新1
execute_cdp_cmd():随着
execute_cdp_cmd(cmd,cmd_args)命令的可用性,现在您可以使用Selenium轻松执行google-chrome-
devtools 命令。使用此功能,您可以轻松修改,以防止检测到Selenium
navigator.webdriver
防止检测 2
为了防止检测到Selenium驱动的 WebDriver ,利基方法将包括以下所有步骤之一或全部:
- 通过命令旋转用户代理
execute_cdp_cmd()
,如下所示:
#Setting up Chrome/83.0.4103.53 as useragent 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'})- 将for webdriver* 的 属性 值更改为 undefined
navigator
***
driver.execute_cdp_cmd("Page.addscriptToevaluateOnNewdocument", { "source": """ Object.defineProperty(navigator, 'webdriver', { get: () => undefined }) """ })排除
enable-automation
开关的集合options.add_experimental_option("excludeSwitches", ["enable-automation"])关掉
useAutomationExtension
options.add_experimental_option('useAutomationExtension', False)
示例代码3
汇总上述所有步骤和有效的代码块,将是:
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'C:WebDriverschromedriver.exe') driver.execute_cdp_cmd("Page.addscriptToevaluateOnNewdocument", { "source": """ 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'}) print(driver.execute_script("return navigator.userAgent;")) driver.get('https://www.httpbin.org/headers')历史
根据 W3C编辑器草案 ,当前的实现严格提到:
当 用户代理 受 远程控制 时,该 标志 设置为,最初设置为。
webdriver-activetrue__false
进一步,
Navigator includes NavigatorAutomationInformation;
要注意的是:
该
NavigatorAutomationInformation接口 不应在 WorkerNavigator 上 公开 。
该
NavigatorAutomationInformation接口 定义为:
interface mixin NavigatorAutomationInformation { readonly attribute boolean webdriver;};true
如果设置了
webdriver-active标志, 则返回,否则返回false。
最后,
navigator.webdriver定义了一种标准方法,用于使用户代理合作以通知文档它由 WebDriver
控制,以便可以在自动化过程中触发备用代码路径。
警告 :更改/调整上述参数可能会阻止 导航 并获取检测到的 WebDriver 实例。
更新(2019年11月6日)
从当前的实现开始,一种理想的访问网页而不被检测到的理想方法是使用
ChromeOptions()该类向以下参数添加几个参数:
- 排除
enable-automation
开关的集合 - 关掉
useAutomationExtension
通过以下实例
ChromeOptions:
- Java示例:
System.setProperty("webdriver.chrome.driver", "C:\Utility\BrowserDrivers\chromedriver.exe"); ChromeOptions options = new ChromeOptions(); options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation")); options.setExperimentalOption("useAutomationExtension", false); WebDriver driver = new ChromeDriver(options); driver.get("https://www.google.com/");- Python范例
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'C:pathtochromedriver.exe') driver.get("https://www.google.com/")传说
1:仅适用于Selenium的Python客户端。
2:仅适用于Selenium的Python客户端。
3:仅适用于Selenium的Python客户端。



