我一直在用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,但并没有尝试。

其他回答

尝试在Chrome的特定用户配置文件中使用Selenium。这样,您就可以作为特定用户使用它,并定义您想要的任何内容。当这样做时,它将作为“真正的”用户运行。用进程资源管理器查看Chrome进程,你会发现标签的不同之处。

例如:

username = os.getenv("USERNAME")
userProfile = "C:\\Users\\" + username +
    "\\AppData\\Local\\Google\\Chrome\\User Data\\Default"

options = webdriver.ChromeOptions()
options.add_argument("user-data-dir={}".format(userProfile))
# Add any tag here you want.
options.add_experimental_option(
    "excludeSwitches",
    """
        ignore-certificate-errors
        safebrowsing-disable-download-protection
        safebrowsing-disable-auto-update
        disable-client-side-phishing-detection
    """.split()
)
chromedriver = "C:\Python27\chromedriver\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
browser = webdriver.Chrome(executable_path=chromedriver, chrome_options=options)

谷歌Chrome标签列表在这里

我还发现一些网站使用了一个检查用户代理的平台。如果该值包含:"HeadlessChrome",则使用无头模式时行为可能会很奇怪。

解决方法是重写用户代理的值,例如在Java中:

chromeOptions.addArguments("--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36");

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

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

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

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

针对一个由Selenium控制的ChromeDriver驱动的网站被检测的问题进行了大量的分析和讨论。以下是我的观点:

根据这篇文章,使用为不同浏览器提供不同网页或服务的用户代理进行浏览器检测通常不是最好的想法之一。无论用户使用哪种浏览器或设备,网络都应该对所有人开放。这里列出了开发网站的最佳实践,以便根据功能可用性而不是针对特定的浏览器逐步增强自己。

然而,浏览器和标准并不完美,仍然有一些边缘情况,一些网站仍然检测到浏览器,如果浏览器是由Selenium控制的WebDriver驱动。浏览器可以通过不同的方式检测,一些常用的机制如下:

实现captcha / recaptcha来检测自动机器人。

您可以在如何recaptcha 3知道我正在使用selenium/chromedriver中找到相关的详细讨论?

检测术语HeadlessChrome在headless Chrome UserAgent

你可以在无头Chrome在Linux上的访问拒绝页面中找到相关的详细讨论,而有头Chrome在通过Python使用Selenium在windows上工作

使用蒸馏网络的机器人管理服务

您可以在无法使用Selenium自动化Chase站点登录中找到相关的详细讨论

使用Akamai的Bot Manager服务

当使用Selenium和Python传递值时,您可以在https://www.nseindia.com/上的动态下拉菜单中找到相关的详细讨论

使用来自Datadome的Bot Protection服务

你可以在网站上找到相关的详细讨论,使用DataDome在使用Selenium和Python抓取时阻止验证码

然而,使用用户代理来检测浏览器看起来很简单,但实际上要做到这一点有点困难。

注意:在这一点上,值得一提的是:使用用户代理嗅探很少是个好主意。总有更好、更广泛兼容的方法来解决某个问题。


浏览器检测的注意事项

检测浏览器背后的想法可以是以下任何一种:

试图解决一个特定的错误在某些特定的变种或特定版本的网络浏览器。 试图检查某些浏览器还不支持的特定功能是否存在。 尝试根据所使用的浏览器提供不同的HTML。


通过UserAgents进行浏览器检测的替代方法

一些浏览器检测的替代方案如下:

Implementing a test to detect how the browser implements the API of a feature and determine how to use it from that. An example was Chrome unflagged experimental lookbehind support in regular expressions. Adapting the design technique of Progressive enhancement which would involve developing a website in layers, using a bottom-up approach, starting with a simpler layer and improving the capabilities of the site in successive layers, each using more features. Adapting the top-down approach of Graceful degradation in which we build the best possible site using all the features we want and then tweak it to make it work on older browsers.


解决方案

为了防止Selenium驱动的WebDriver被检测到,一个合适的方法应该包括下面提到的方法中的任何一种或所有方法:

Rotating the UserAgent in every execution of your Test Suite using fake_useragent module as follows: from selenium import webdriver from selenium.webdriver.chrome.options import Options from fake_useragent import UserAgent options = Options() ua = UserAgent() userAgent = ua.random print(userAgent) options.add_argument(f'user-agent={userAgent}') driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\ChromeDriver\chromedriver_win32\chromedriver.exe') driver.get("https://www.google.co.in") driver.quit()

您可以在如何在Selenium中更改谷歌Chrome用户代理中找到相关的详细讨论?

Rotating the UserAgent in each of your Tests using Network.setUserAgentOverride through execute_cdp_cmd() as follows: from selenium import webdriver driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe') print(driver.execute_script("return navigator.userAgent;")) # Setting user agent as Chrome/83.0.4103.97 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.97 Safari/537.36'}) print(driver.execute_script("return navigator.userAgent;"))

您可以在如何使用Selenium和Python更改用户代理中找到相关的详细讨论

修改webdriver的navigator属性值为undefined,如下所示: driver.execute_cdp_cmd(“页面。addScriptToEvaluateOnNewDocument”,{ “源”:“” Object.defineProperty(导航器,'webdriver', { Get: () => undefined }) ”“” })

您可以在Selenium webdriver:修改导航器中找到相关的详细讨论。Webdriver标志,以防止硒检测

更改navigator的值。插件,导航器。语言,WebGL,发际线功能,缺失图像等。

您可以在“是否存在无法检测到的selenium webdriver版本?”中找到相关的详细讨论。

改变传统的视口

您可以在如何绕过谷歌验证码与硒和python中找到相关的详细讨论?


处理reCAPTCHA

在处理2captcha和recaptcha-v3而不是点击与文本相关的复选框时,我不是机器人,提取和使用data-sitekey可能更容易获得身份验证。

您可以在如何识别ReCaptcha V2的32位data-sitekey中找到相关的详细讨论,以使用Selenium和Python请求以编程方式获得有效的响应?


Tl;博士

你可以找到一个先进的解决方案,以逃避网络驱动检测:

硒隐身-一种行之有效的方法来逃避网络驱动检测

随着硒隐身的可用性,逃避硒驱动的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没有正确加载