我一直在用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。


当前回答

我所要做的就是

my_options = webdriver.ChromeOptions()
my_options.add_argument( '--disable-blink-features=AutomationControlled' )

Some more information to this: This relates to website skyscanner.com. In the past I have been able to scrape it. Yes, it did detect the browser automation and it gave me a captcha to press and hold a button. I used to be able to complete the captcha manually, then search flights and then scrape. But this time around after completing the captcha I get the same captcha again and again, just can't seem to escape from it. I tried some of the most popular suggestions to avoid automation being detected, but they didn't work. Then I found this article which did work, and by process of elimination I found out it only took the option above to get around their browser automation detection. Now I don't even get the captcha and everything else seems to be working normally.

我目前正在运行的版本:

操作系统:Windows 7 64位 win32上的Python 3.8.0 (tags/v3.8.0:fa919fd, 2019-10-14) (MSC v.1916 64位(AMD64)) 浏览器:Chrome版本100.0.4896.60(官方 构建)(64位) 4.1.3硒 ChromeDriver 100.0.4896.60 chromedriver_win32.zip 930ff33ae8babeaa74e0dd1ce1dae7ff

其他回答

我所要做的就是

my_options = webdriver.ChromeOptions()
my_options.add_argument( '--disable-blink-features=AutomationControlled' )

Some more information to this: This relates to website skyscanner.com. In the past I have been able to scrape it. Yes, it did detect the browser automation and it gave me a captcha to press and hold a button. I used to be able to complete the captcha manually, then search flights and then scrape. But this time around after completing the captcha I get the same captcha again and again, just can't seem to escape from it. I tried some of the most popular suggestions to avoid automation being detected, but they didn't work. Then I found this article which did work, and by process of elimination I found out it only took the option above to get around their browser automation detection. Now I don't even get the captcha and everything else seems to be working normally.

我目前正在运行的版本:

操作系统:Windows 7 64位 win32上的Python 3.8.0 (tags/v3.8.0:fa919fd, 2019-10-14) (MSC v.1916 64位(AMD64)) 浏览器:Chrome版本100.0.4896.60(官方 构建)(64位) 4.1.3硒 ChromeDriver 100.0.4896.60 chromedriver_win32.zip 930ff33ae8babeaa74e0dd1ce1dae7ff

用下面的代码编写一个HTML页面。你会看到在DOM中selenium应用了outerHTML中的webdriver属性:

< html > < >头 < script type = " text / javascript " > < !-- 函数显示窗口(){ javascript:(警报(document.documentElement.outerHTML)); } //--> > < /脚本 > < /头 身体< > < >形式 <input type="button" value="Show outerHTML" onclick="showWindow()" > > < /形式 身体< / > < / html >

一些网站发现了这一点:

function d() {
try {
    if (window.document.$cdc_asdjflasutopfhvcZLmcfl_.cache_)
        return !0
} catch (e) {}

try {
    //if (window.document.documentElement.getAttribute(decodeURIComponent("%77%65%62%64%72%69%76%65%72")))
    if (window.document.documentElement.getAttribute("webdriver"))
        return !0
} catch (e) {}

try {
    //if (decodeURIComponent("%5F%53%65%6C%65%6E%69%75%6D%5F%49%44%45%5F%52%65%63%6F%72%64%65%72") in window)
    if ("_Selenium_IDE_Recorder" in window)
        return !0
} catch (e) {}

try {
    //if (decodeURIComponent("%5F%5F%77%65%62%64%72%69%76%65%72%5F%73%63%72%69%70%74%5F%66%6E") in document)
    if ("__webdriver_script_fn" in document)
        return !0
} catch (e) {}

替换cdc_ string

您可以使用Vim或Perl来替换chromedriver中的cdc_ string。查看@Erti-Chris Eelmaa的回答,了解更多关于字符串的信息,以及它是如何成为探测点的。

使用Vim或Perl可以避免重新编译源代码或使用十六进制编辑器。

在尝试编辑原始chromedriver之前,请确保复制它。

我们的目标是修改cdc_字符串,它看起来类似于$cdc_lasutopfhvcZLmcfl。

下面的方法在chromedriver版本2.41.578706上进行了测试。


使用Vim

vim /path/to/chromedriver

运行上面的代码行之后,您可能会看到一堆胡言乱语。做以下几点:

通过输入:%s/cdc_/dog_/g将cdc_的所有实例替换为dog_。 Dog_只是一个例子。你可以选择任何东西,只要它有相同数量的字符作为搜索字符串(例如,cdc_),否则chromedriver将失败。 要保存修改并退出,输入:wq!然后按回车键。 如果您需要在不保存更改的情况下退出,请键入:q!然后按回车键。


使用Perl

下面这行代码将所有cdc_事件替换为dog_。Vic Seedoubleyew:

perl -pi -e 's/cdc_/dog_/g' /path/to/chromedriver

确保替换字符串(例如,dog_)与搜索字符串(例如,cdc_)具有相同的字符数,否则chromedriver将失败。


结束

验证所有出现的cdc_已被替换:

grep "cdc_" /path/to/chromedriver

如果没有返回输出,则替换成功。

转到修改过的chromedriver,双击它。这时应该会打开一个终端窗口。如果您在输出中没有看到killed,那么您已经成功修改了驱动程序。

确保更改后的chromedriver二进制文件的名称为chromedriver,并且原始二进制文件要么从原始位置移动,要么重命名。


我使用这种方法的经验

我之前在尝试登录时在网站上被检测到,但在用相等大小的字符串替换cdc_后,我能够登录。就像其他人说的那样,如果你已经被检测到,即使使用了这种方法,你也可能会因为其他很多原因被屏蔽。因此,您可能需要尝试访问使用VPN、其他网络等检测您的站点。

混淆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,所以应该有更多的变量需要改变。下一步应该是对我不想做的检测器服务进行逆向工程。

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