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


当前回答

Chromium开发人员最近在2021年增加了第二个无头模式,不再将HeadlessChrome添加到用户代理字符串中。看到https://bugs.chromium.org/p/chromium/issues/detail?id=706008 c36

他们后来在2023年为Chrome 109重命名了该选项-> https://github.com/chromium/chromium/commit/e9c516118e2e1923757ecb13e6d9fff36775d1f4

新的——headless=new标志现在可以让你在新的无头模式下获得Chrome的全部功能,你甚至可以在这个模式下运行Chrome 109及以上版本的扩展。(如果使用Chrome 96到108,使用旧的——headless= Chrome选项。)

用法:(Chrome 109及以上):

options.add_argument("--headless=new")

用法:(Chrome 96到Chrome 108):

options.add_argument("--headless=chrome")

这种新的无头模式使Chrome浏览器像普通模式一样工作,这意味着它们不像旧版无头模式的Chrome浏览器那样容易被检测到。

将其与其他工具(如未检测的chromedriver)结合起来,以最大限度地逃避硒检测。

其他回答

我所要做的就是

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

它适用于一些网站,从导航器中删除属性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;"
    })

在我看来,用Selenium做这件事最简单的方法是拦截发送回浏览器指纹的XHR。

但由于这是一个只有硒的问题,所以最好使用其他东西。硒应该让事情变得更容易,而不是更难。

一些网站发现了这一点:

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) {}

针对一个由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;博士

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

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