我正在尝试使用selenium和铬在网站中自动化一个非常基本的任务,但是以某种方式网站会检测到铬是由selenium驱动的,并阻止每个请求。我怀疑该网站是否依赖像这样的公开DOM变量来检测selenium驱动的浏览器。
我的问题是,有没有办法使navigator.webdriver标志为假?我愿意尝试修改后重新尝试编译selenium源,但似乎无法在存储库中的任何地方找到NavigatorAutomationInformation源https://github.com/SeleniumHQ/selenium
任何帮助深表感谢
PS:我还从https://w3c.github.io/webdriver/#interface尝试了以下操作
Object.defineProperty(navigator, 'webdriver', {
get: () => false,
});
但是它仅在初始页面加载后更新属性。我认为网站会在执行脚本之前检测到变量。
execute_cdp_cmd()
:随着execute_cdp_cmd(cmd,
cmd_args)
命令的可用性,现在您可以使用Selenium轻松执行google-chrome-
devtools 命令。使用此功能,您可以轻松修改,以防止检测到Seleniumnavigator.webdriver
为了防止检测到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'})
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)
汇总上述所有步骤和有效的代码块,将是:
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:\WebDrivers\chromedriver.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-active
true
__false
进一步,
Navigator includes NavigatorAutomationInformation;
要注意的是:
该
NavigatorAutomationInformation
接口 不应在 WorkerNavigator 上 公开 。
该NavigatorAutomationInformation
接口 定义为:
interface mixin NavigatorAutomationInformation {
readonly attribute boolean webdriver;
};
true
如果设置了webdriver-active
标志, 则返回,否则返回false。
最后,navigator.webdriver
定义了一种标准方法,用于使用户代理合作以通知文档它由 WebDriver
控制,以便可以在自动化过程中触发备用代码路径。
警告 :更改/调整上述参数可能会阻止 导航 并获取检测到的 WebDriver 实例。
从当前的实现开始,一种理想的访问网页而不被检测到的理想方法是使用ChromeOptions()
该类向以下参数添加几个参数:
enable-automation
开关的集合useAutomationExtension
通过以下实例ChromeOptions
:
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/");
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\to\chromedriver.exe')
driver.get("https://www.google.com/")
1:仅适用于Selenium的Python客户端。
2:仅适用于Selenium的Python客户端。
3:仅适用于Selenium的Python客户端。