chromedriver window.navigator.webdriver标志在chrome v80 +中为true [英] chromedriver window.navigator.webdriver flag is true with chrome v80+

查看:235
本文介绍了chromedriver window.navigator.webdriver标志在chrome v80 +中为true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用硒chromeDriver进行自动化测试.但是我不能隐藏" window.navigator.webdriver属性.当我通过驱动程序打开chrome(v64-80.0.3987.100)时,在控制台中查看时始终返回true.这是我的代码:

I am using selenium chromeDriver for automated testing. But I cannot "hide" the window.navigator.webdriver property. When I open chrome(v64-80.0.3987.100) through the driver, it always returns true when viewed in the console. Here is my code:

System.setProperty("webdriver.firefox.marionette", "false");
System.setProperty("webdriver.chrome.driver", webDriverPath);
ChromeOptions options = new ChromeOptions();
options.addArguments("--no-sandbox");
options.addArguments("--disable-extensions");
options.addArguments("--blink-settings=imagesEnabled=false");
options.addArguments("--disable-infobars");
options.addArguments("--disable-dev-shm-usage");
options.setExperimentalOption("useAutomationExtension", false);
options.addArguments("--incognito");
options.addArguments("--start-maximized");
options.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"});
options.addArguments("--lang=zh-CN,zh,en");
options.addArguments("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36");
options.addArguments("--window-size=1920,1080");
ChromeDriver driver = new ChromeDriver(options);
driver.executeScript("Object.defineProperty(navigator, \"webdriver\", {\n" + "    get()  { return undefined; },\n" + "    set(v) { return v; },\n" + "    configurable: true,\n" + "    enumerable: false\n" + "});");
driver.manage().timeouts().pageLoadTimeout(30L, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(5L, TimeUnit.SECONDS);

如果我使用较低版本的chrome(例如v64-76.0.3809.100),它将返回undefined.谁能帮我吗?

If I use a lower version of chrome (like v64-76.0.3809.100), it returns undefined. Can anyone help me?

推荐答案

window.navigator.webdriver属性的存在由 W3C WebDriver规范.它既是安全功能,又是使网页具有更好的可测试性的功能.如果您尝试使用由WebDriver驱动的机器人使页面自动化,而您不拥有该页面被自动化,并且您试图向页面所有者隐藏您正在使用机器人的事实,即恰好该属性打算公开的东西.

The presence of the window.navigator.webdriver property is mandated by the W3C WebDriver Specification. It is both a security feature and a feature to allow better testability of web pages. If you are attempting to automate a page with a WebDriver-powered bot, and you do not own the page being automated, and you are attempting to hide the fact that you’re using a bot from the page’s owners, that is exactly the kind of thing that the property is intended to expose.

Chrome和chromedriver的开发人员现在似乎已经关闭了他们在先前版本中允许的安全漏洞.我认为您可能会构建自己的黑版浏览器和驱动程序,以在使用WebDriver浏览网站时不设置该属性,但这是唯一的方法.

It looks like the developers of Chrome and chromedriver have now closed the security loophole they had allowed in previous versions. I suppose it’s possible you could build your own hacked version of the browser and the driver to not set that property when browsing sites with WebDriver, but that would be the only way.

作为社论评论,试图隐藏由WebDriver驱动的bot进行的浏览通常是傻子的事.除了navigator.webdriver属性以外,还有多种方法可以检测到这种bot,几乎不可能完全屏蔽它们.即使它似乎可以工作一段时间,最终,该站点也会提供更多的检测机制.此外,这种活动极有可能违反几乎任何人都愿意尝试这样做的站点的服务条款.最好找到另一个实现目标的机制.

As an editorial comment, attempting to hide browsing by a WebDriver-powered bot is most often a fool’s errand. There are multiple ways of detecting such a bot beyond the navigator.webdriver property, and it would be nearly impossible to mask them all. Even if it appeared to work for awhile, eventually, the site in question will come up with more detection mechanisms. Moreover, such activity more than likely violates the Terms of Service of nearly any site one would care to attempt this for. It would be far better to find another mechanism for accomplishing your goal.

这篇关于chromedriver window.navigator.webdriver标志在chrome v80 +中为true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆