Selenium webdriver:修改 navigator.webdriver 标志以防止 selenium 检测 [英] Selenium webdriver: Modifying navigator.webdriver flag to prevent selenium detection
问题描述
我正在尝试使用 selenium 和 chrome 在网站中自动执行一项非常基本的任务,但是该网站以某种方式检测到 chrome 何时由 selenium 驱动并阻止每个请求.我怀疑该网站依赖于这样一个公开的 DOM 变量 https://stackoverflow.com/a/41904453/648236a> 检测 selenium 驱动的浏览器.
I'm trying to automate a very basic task in a website using selenium and chrome but somehow the website detects when chrome is driven by selenium and blocks every request. I suspect that the website is relying on an exposed DOM variable like this one https://stackoverflow.com/a/41904453/648236 to detect selenium driven browser.
我的问题是,有没有办法让 navigator.webdriver 标志为假?我愿意在修改后尝试重新编译 selenium 源,但我似乎无法在存储库中的任何位置找到 NavigatorAutomationInformation 源 https://github.com/SeleniumHQ/selenium
My question is, is there a way I can make the navigator.webdriver flag false? I am willing to go so far as to try and recompile the selenium source after making modifications, but I cannot seem to find the NavigatorAutomationInformation source anywhere in the repository https://github.com/SeleniumHQ/selenium
非常感谢任何帮助
PS:我还尝试了以下来自 https://w3c.github.io/webdriver/#界面
P.S: I also tried the following from https://w3c.github.io/webdriver/#interface
Object.defineProperty(navigator, 'webdriver', {
get: () => false,
});
但它只在初始页面加载后更新属性.我认为该站点会在执行我的脚本之前检测到该变量.
But it only updates the property after the initial page load. I think the site detects the variable before my script is executed.
推荐答案
先更新1
execute_cdp_cmd()
:随着 execute_cdp_cmd(cmd, cmd_args)
命令的可用性,您现在可以轻松地执行 google-chrome-devtools 命令 使用 硒.使用此功能,您可以轻松修改 navigator.webdriver
以防止 Selenium 被检测到.
First the update 1
execute_cdp_cmd()
: With the availability of execute_cdp_cmd(cmd, cmd_args)
command now you can easily execute google-chrome-devtools commands using Selenium. Using this feature you can modify the navigator.webdriver
easily to prevent Selenium from getting detected.
为了防止 Selenium 驱动的 WebDriver 被检测到,一种利基方法将包括以下提到的任一/所有步骤:
To prevent Selenium driven WebDriver getting detected a niche approach would include either/all of the below mentioned steps:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--disable-blink-features=AutomationControlled')
driver = webdriver.Chrome(options=options, executable_path=r'C:WebDriverschromedriver.exe')
driver.get("https://www.website.com")
旋转user-agent通过
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'})
将webdriver的
navigator
的属性值更改为未定义强>driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
排除
enable-automation
开关的集合options.add_experimental_option("excludeSwitches", ["enable-automation"])
关闭
useAutomationExtension
options.add_experimental_option('useAutomationExtension', False)
将上面提到的所有步骤和有效的代码块组合起来将是:
Clubbing up all the steps mentioned above and effective code block will be:
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_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'})
print(driver.execute_script("return navigator.userAgent;"))
driver.get('https://www.httpbin.org/headers')
历史
根据 W3C 编辑草案,当前的实现严格提到:
History
As per the W3C Editor's Draft the current implementation strictly mentions:
webdriver-active
flag 设置为 true
当用户代理在远程控制之下,最初设置为false
.
The
webdriver-active
flag is set totrue
when the user agent is under remote control which is initially set tofalse
.
进一步,
Navigator includes NavigatorAutomationInformation;
需要注意的是:
NavigatorAutomationInformation
接口不应暴露在 WorkerNavigator 上.
NavigatorAutomationInformation
接口定义为:
interface mixin NavigatorAutomationInformation {
readonly attribute boolean webdriver;
};
如果设置了 webdriver-active
flag,则返回 true
,否则返回 false.
which returns true
if webdriver-active
flag is set, false otherwise.
最后,navigator.webdriver
定义了一种标准方式,用于协作用户代理来通知文档它是由 WebDriver 控制的,以便替代代码路径可以在自动化过程中被触发.
Finally, the navigator.webdriver
defines a standard way for co-operating user agents to inform the document that it is controlled by WebDriver, so that alternate code paths can be triggered during automation.
注意:更改/调整上述参数可能会阻止导航并检测到 WebDriver 实例.
Caution: Altering/tweaking the above mentioned parameters may block the navigation and get the WebDriver instance detected.
更新(2019 年 11 月 6 日)
在当前实现中,访问网页而不被检测到的理想方法是使用 ChromeOptions()
类向以下内容添加几个参数:
Update (6-Nov-2019)
As of the current implementation an ideal way to access a web page without getting detected would be to use the ChromeOptions()
class to add a couple of arguments to:
- 排除
enable-automation
开关的集合 - 关闭
useAutomationExtension
- Exclude the collection of
enable-automation
switches - Turn-off
useAutomationExtension
通过一个ChromeOptions
的实例如下:
Java 示例:
Java Example:
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 示例
Python Example
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:path ochromedriver.exe')
driver.get("https://www.google.com/")
1:仅适用于 Selenium 的 Python 客户端.
1: Applies to Selenium's Python clients only.
2:仅适用于 Selenium 的 Python 客户端.
2: Applies to Selenium's Python clients only.
3:仅适用于 Selenium 的 Python 客户端.
3: Applies to Selenium's Python clients only.
这篇关于Selenium webdriver:修改 navigator.webdriver 标志以防止 selenium 检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!