我一直在用Chromedriver测试Selenium,我注意到一些页面可以检测到你正在使用Selenium,即使根本没有自动化。甚至当我手动使用Chrome通过Selenium和Xephyr浏览时,我经常会看到一个页面说检测到可疑活动。我已经检查了我的用户代理和浏览器指纹,它们都与正常的Chrome浏览器完全相同。

当我在普通的Chrome浏览器中浏览这些网站时,一切都很好,但当我使用Selenium时,我被检测到。

理论上,chromedriver和Chrome在任何web服务器上看起来应该是完全一样的,但不知何故它们可以检测到它。

如果你想要一些测试代码,试试这个:

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=1, size=(1600, 902))
display.start()
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--profile-directory=Default')
chrome_options.add_argument("--incognito")
chrome_options.add_argument("--disable-plugins-discovery");
chrome_options.add_argument("--start-maximized")
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.delete_all_cookies()
driver.set_window_size(800,800)
driver.set_window_position(0,0)
print 'arguments done'
driver.get('http://stubhub.com')

如果你在stubhub周围浏览,你会在一两个请求内被重定向和“阻止”。我一直在研究这个问题,但我不知道他们是如何判断用户正在使用Selenium的。

他们是怎么做到的?

我在Firefox中安装了Selenium IDE插件,当我在普通的Firefox浏览器中只使用附加插件访问stubhub.com时,我被禁止了。

当我使用Fiddler查看来回发送的HTTP请求时,我注意到“假浏览器”的请求经常在响应头中有“无缓存”。

是否有一种方法可以从JavaScript检测我是否在Selenium Webdriver页面中?建议当你在使用网络驱动程序时没有办法检测。但这些证据表明情况并非如此。

该网站将指纹上传到他们的服务器上,但我检查了一下,Selenium的指纹与使用Chrome时的指纹是相同的。

这是他们发送到服务器上的指纹载荷之一:

{"appName":"Netscape","platform":"Linuxx86_64","cookies":1,"syslang":"en-US","userlang":"en-
US","cpu":"","productSub":"20030107","setTimeout":1,"setInterval":1,"plugins":
{"0":"ChromePDFViewer","1":"ShockwaveFlash","2":"WidevineContentDecryptionMo
dule","3":"NativeClient","4":"ChromePDFViewer"},"mimeTypes":
{"0":"application/pdf","1":"ShockwaveFlashapplication/x-shockwave-
flash","2":"FutureSplashPlayerapplication/futuresplash","3":"WidevineContent
DecryptionModuleapplication/x-ppapi-widevine-
cdm","4":"NativeClientExecutableapplication/x-
nacl","5":"PortableNativeClientExecutableapplication/x-
pnacl","6":"PortableDocumentFormatapplication/x-google-chrome-
pdf"},"screen":{"width":1600,"height":900,"colorDepth":24},"fonts":
{"0":"monospace","1":"DejaVuSerif","2":"Georgia","3":"DejaVuSans","4":"Trebu
chetMS","5":"Verdana","6":"AndaleMono","7":"DejaVuSansMono","8":"LiberationM
ono","9":"NimbusMonoL","10":"CourierNew","11":"Courier"}}

它在Selenium和Chrome中是相同的。

vpn只用于一次使用,但在加载第一个页面后就会被检测到。显然,正在运行一些JavaScript代码来检测Selenium。


当前回答

答:是的

有些网站会通过浏览器的指纹和其他数据来检测硒,还有一些网站会根据行为来检测硒,不仅根据你做了什么,还根据你没有做什么。

通常用硒提供的数据就足以检测它。

你可以在像这样的网站上查看浏览器指纹

https://bot.sannysoft.com
https://fingerprintjs.github.io/fingerprintjs/
https://antoinevastel.com/bots/

尝试使用您的用户浏览器,然后尝试使用selenium,您将看到差异。

您可以使用options()更改一些指纹,如用户代理和其他,自己查看结果。

你可以尝试用很多方法来避免这种检测,我建议使用以下库:

https://github.com/ultrafunkamsterdam/undetected-chromedriver

import undetected_chromedriver.v2 as uc

否则你可以尝试使用硒的替代品。我听说过PhantomJS,但并没有尝试。

其他回答

混淆JavaScript结果

我已经检查了chromedriver的源代码。这将向浏览器中注入一些JavaScript文件。 这个链接中的每个JavaScript文件都被注入到网页中: https://chromium.googlesource.com/chromium/src/+/master/chrome/test/chromedriver/js/

所以我使用了逆向工程,并通过十六进制编辑来混淆JavaScript文件。现在我确信不再使用JavaScript变量、函数名和固定字符串来揭示硒活动。但是仍然有一些站点和reCAPTCHA检测到硒!

也许他们会检查由chromedriver JavaScript执行引起的修改:)

Chrome 'navigator'参数修改

我发现在“导航器”中有一些参数,简要地揭示使用chromedriver。

这些是参数:

“导航器。在非自动化模式下,它是“未定义的”。在自动模式下,它是“正确的”。 “导航器。在无头Chrome中,它的长度为0。所以我添加了一些假元素来欺骗插件长度检查过程。 “导航器。["en- us ", "en", "es"]]。

所以我需要的是一个chrome扩展在网页上运行JavaScript。我使用本文提供的JavaScript代码制作了一个扩展,并使用另一篇文章将压缩后的扩展添加到我的项目中。我已经成功地更改了值;但是还是没有任何变化!

我没有找到其他类似的变量,但这并不意味着它们不存在。reCAPTCHA仍然检测chromedriver,所以应该有更多的变量需要改变。下一步应该是对我不想做的检测器服务进行逆向工程。

现在我不确定是否值得在这个自动化过程上花费更多的时间或寻找替代方法!

除此之外,Erti-Chris Eelmaa给出了一个很好的答案——恼人的window.navigator.webdriver,而且它是只读的。即使你把它的值改为false,它仍然是true。这就是为什么由自动化软件驱动的浏览器仍然可以被检测到。

MDN

该变量由chrome中的——enable-automation标志管理。chromedriver启动Chrome时带有这个标志,Chrome将window.navigator.webdriver设置为true。你可以在这里找到它。您需要添加“排除开关”标志。例如(Go):

package main

import (
    "github.com/tebeka/selenium"
    "github.com/tebeka/selenium/chrome"
)

func main() {

caps := selenium.Capabilities{
    "browserName": "chrome",
}

chromeCaps := chrome.Capabilities{
    Path:            "/path/to/chrome-binary",
    ExcludeSwitches: []string{"enable-automation"},
}
caps.AddChrome(chromeCaps)

wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", 4444))
}

我所看到的机器人检测似乎更复杂,或者至少与我在下面阅读的答案不同。

实验1

我从Python控制台用Selenium打开浏览器和网页。 鼠标已经在一个特定的位置,我知道一旦页面加载,就会出现一个链接。我从不移动鼠标。 我按一次鼠标左键(这对于将焦点从运行Python的控制台转移到浏览器是必要的)。 我再次按下鼠标左键(记住,光标在给定链接的上方)。 链接正常打开,就像它应该的那样。

实验2

像以前一样,我从Python控制台打开一个浏览器和带有Selenium的web页面。 这一次,我不再使用鼠标单击,而是使用Selenium(在Python控制台中)单击带有随机偏移量的同一元素。 链接没有打开,但我被带到一个注册页面。

影响

通过Selenium打开网页浏览器并不妨碍我看起来像人类 像人类一样移动鼠标并不一定要被归类为人类 通过Selenium点击带有偏移量的内容仍然会引起警报

这看起来很神秘,但我猜他们可以确定一个操作是否起源于Selenium,而他们并不关心浏览器本身是否是通过Selenium打开的。或者他们能确定窗口是否有焦点吗?如果有人有什么见解,我会很有趣。

正如我们已经在问题和发布的答案中发现的那样,这里有一个名为“蒸馏网络”的反网络抓取和机器人检测服务。根据该公司CEO的采访:

尽管他们可以创造新的机器人,但我们找到了识别的方法 硒是他们使用的一个工具,所以我们阻止硒不 不管他们在机器人上迭代了多少次。我们现在正在做 使用Python和许多不同的技术。一旦我们发现了规律 从一种机器人中脱颖而出,然后我们对其进行逆向工程 他们使用的技术并将其识别为恶意的。

要了解他们究竟是如何检测硒的,还需要时间和更多的挑战,但目前我们可以肯定地说:

it's not related to the actions you take with Selenium. Once you navigate to the site, you get immediately detected and banned. I've tried to add artificial random delays between actions, take a pause after the page is loaded - nothing helped it's not about browser fingerprint either. I tried it in multiple browsers with clean profiles and not, incognito modes, but nothing helped since, according to the hint in the interview, this was "reverse engineering", I suspect this is done with some JavaScript code being executed in the browser revealing that this is a browser automated via Selenium WebDriver

我决定把它作为一个答案,因为很明显:

当你用chromedriver使用selenium时,网站能检测到吗?

Yes.


Also, I haven't experimented with older Selenium and older browser versions. In theory, there could be something implemented/added to Selenium at a certain point that Distil Networks bot detector currently relies on. Then, if this is the case, we might detect (yeah, let's detect the detector) at what point/version a relevant change was made, look into changelog and changesets and, may be, this could give us more information on where to look and what is it they use to detect a webdriver-powered browser. It's just a theory that needs to be tested.

它适用于一些网站,从导航器中删除属性webdriver

from selenium import webdriver
driver = webdriver.Chrome()
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
    "source":
        "const newProto = navigator.__proto__;"
        "delete newProto.webdriver;"
        "navigator.__proto__ = newProto;"
    })