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

其他回答

你可以尝试使用参数enable-automation

var options = new ChromeOptions();

// hide selenium
options.AddExcludedArguments(new List<string>() { "enable-automation" });

var driver = new ChromeDriver(ChromeDriverService.CreateDefaultService(), options);

但是,我想警告一下,这个能力在ChromeDriver 79.0.3945.16中被修复了。 所以你可能应该使用旧版本的chrome。

另外,作为另一种选择,你可以尝试使用InternetExplorerDriver而不是Chrome。对我来说,IE在没有任何hack的情况下根本不会阻塞。

想要了解更多信息,请看看这里:

Selenium webdriver:修改导航器。Webdriver标志,以防止硒检测

在Chrome v76中无法隐藏“Chrome被自动软件控制”信息栏

部分接口导航器{只读属性布尔webdriver;};

Navigator接口的webdriver IDL属性必须返回webdriver-active标志的值,初始值为false。

此属性允许网站确定用户代理受WebDriver控制,并可用于帮助减轻拒绝服务攻击。

直接摘自2017年W3C WebDriver编辑草案。这在很大程度上意味着,至少Selenium驱动程序的未来迭代将是可识别的,以防止误用。最终,如果没有源代码,就很难判断到底是什么原因导致chrome驱动程序被检测到。

随着硒隐身的可用性,逃避硒驱动的ChromeDriver启动谷歌chrome浏览上下文的检测已经变得更加容易。


selenium-stealth

selenium-stealth是一个Python包,用于防止被探测。这个程序试图使python selenium更加隐秘。然而,目前硒隐身只支持硒铬。

目前硒隐身可以提供的特性:

具有隐身功能的硒隐身通过了所有公共机器人测试。 用硒隐身硒可以做到谷歌账号登录。 硒隐身有助于保持正常的reCAPTCHA v3评分


安装

硒隐身在PyPI上可用,因此您可以按照以下方式安装pip:

pip install selenium-stealth

Selenium4兼容代码

Code Block: from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium_stealth import stealth options = Options() options.add_argument("start-maximized") # Chrome is controlled by automated test software options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) s = Service('C:\\BrowserDrivers\\chromedriver.exe') driver = webdriver.Chrome(service=s, options=options) # Selenium Stealth settings stealth(driver, languages=["en-US", "en"], vendor="Google Inc.", platform="Win32", webgl_vendor="Intel Inc.", renderer="Intel Iris OpenGL Engine", fix_hairline=True, ) driver.get("https://bot.sannysoft.com/") Browser Screenshot:


Tl;博士

你可以在下面找到一些相关的详细讨论:

当你用chromedriver使用Selenium时,网站能检测到吗? 如何自动登录到一个网站,这是检测我的尝试登录使用硒隐身 未检测到的Chromedriver没有正确加载

听起来他们就像是在网络应用防火墙后面。看看modsecurity和OWASP,看看它们是如何工作的。

实际上,您要问的是如何进行机器人检测逃避。这不是Selenium WebDriver的目的。它是用来测试你的web应用程序,而不影响其他web应用程序。这是可能的,但基本上,您必须查看WAF在其规则集中寻找什么,如果可以的话,特别避免使用selenium。即使这样,它仍然可能不起作用,因为您不知道他们使用的是什么WAF。

您做了正确的第一步,即伪造用户代理。如果这不能工作,那么WAF是合适的,你可能需要变得更棘手。

这一点来自其他答案。首先要确保正确地设置了用户代理。也许让它攻击本地网络服务器或嗅探流出的流量。

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

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