Chrome如何设定webdriver=undefined以制止Selenium检测?
标签:
Chrome如何设定webdriver=undefined以制止Selenium检测? 一、WebDriver规范按照的描述,,WebDriver界说了一个标准要领,以便于文档(document)判断当前浏览器处于自动化控制之中。
这个要领就是检测window.navigator.webdriver的值,正常情况下其值为undefined,自动化控制下为true。注意,正常情况下不是false,在JavaScript中undefined为不决义,即该值不存在,而false暗示一布尔值。
附上规范原文:
The webdriver-active flag is set to true when the user agent is under remote control. It is initially false.
Defines a standard way for co-operating user agents to inform the document that it is controlled by WebDriver, for example so that alternate code paths can be triggered during automation.
二、打破ChromeDriver的设计切合这一规范,如何打破?
旧版本在版本79.0.3945.16之前,可用如下要领:
#!/usr/bin/env python3 # -*- coding: UTF-8 -*- 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) driver.get("YOUR_URL") # 在控制台中验证window.navigator.webdriver的值为undefined。 driver.quit() 新版本在版本79.0.3945.16之后,ChromeDriver修正了这一“问题”。
按照注记原文:
Resolved issue 3133: window.navigator.webdriver is undefined when "enable-automation" is excluded in non-headless mode (should be true) [Pri-2]
如何打破?
execute_cdp_cmd函数来资助!cdp即Chrome DevTools Protocal,Chrome开发者工具协议。
通过该函数在文档加载前注入一段js代码以消去webdriver值。
#!/usr/bin/env python3 # -*- coding: UTF-8 -*- from selenium import webdriver driver = webdriver.Chrome() script = ''' Object.defineProperty(navigator, 'webdriver', { get: () => undefined }) ''' driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {"source": script}) driver.get("YOUR_URL") # 在控制台中验证window.navigator.webdriver的值为undefined。 driver.quit()Chrome如何设定webdriver=undefined以制止Selenium检测?
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/32189.html