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


当前回答

我还发现一些网站使用了一个检查用户代理的平台。如果该值包含:"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");

其他回答

即使你发送了所有正确的数据(例如,Selenium没有显示为扩展,你有一个合理的分辨率/位深,等等),也有许多服务和工具可以分析访问者的行为,以确定参与者是用户还是自动化系统。

例如,访问一个网站,然后立即通过将鼠标直接移动到相关按钮上来执行一些操作,在不到一秒钟的时间内,这是用户实际上不会做的事情。

它也可能是一个有用的调试工具,使用网站,如https://panopticlick.eff.org/,以检查您的浏览器有多独特;它还将帮助您验证是否有任何特定的参数表明您正在运行Selenium。

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

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

尝试在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标签列表在这里

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