
优化Selenium Chromedriver以绕过反爬虫检测
undetected-chromedriver是一个优化的Selenium Chromedriver补丁,可绕过多种反爬虫服务的检测。它自动下载并修补驱动程序,支持最新Chrome版本,适用于Python 3.6+。该项目可用于多种基于Chromium的浏览器,提供简单API,有效避免被识别为自动化工具。对Web自动化和爬虫项目具有重要价值。
https://github.com/ultrafunkamsterdam/undetected-chromedriver
Optimized Selenium Chromedriver patch which does not trigger anti-bot services like Distill Network / Imperva / DataDome / Botprotect.io Automatically downloads the driver binary and patches it.
pip install undetected-chromedriver
or , if you're feeling adventurous, install directly via github
pip install git+https://www.github.com/ultrafunkamsterdam/undetected-chromedriver@master # replace @master with @branchname for other branches
I will be putting limits on the issue tracker. It has beeen abused too long.
any good news?
Yes, i've opened Undetected-Discussions which i think will help us better in the long run.
THIS PACKAGE DOES NOT, and i repeat DOES NOT hide your IP address, so when running from a datacenter (even smaller ones), chances are large you will not pass! Also, if your ip reputation at home is low, you won't pass!
Running following code from home , and from a datacenter.
<div style="display:flex;flex-direction:row"> <img src="https://github.com/ultrafunkamsterdam/undetected-chromedriver/assets/21027969/262dad3e-33e9-4d67-b061-b30bc74ac9bc" width="720"/> <img src="https://github.com/ultrafunkamsterdam/undetected-chromedriver/assets/21027969/5e1d463b-3f88-496a-9a43-a39830f909da" width="720"/> </div> <!--  --> <!--  -->import undetected_chromedriver as uc driver = uc.Chrome(headless=True,use_subprocess=False) driver.get('https://nowsecure.nl') driver.save_screenshot('nowsecure.png')
Big update! be careful as it -potentially- could break your code.
rewritten the anti-detection mechanism instead of removing and renaming variables, we just keep them, but prevent them from being injected in the first place. This will keep us safe from detection at least for the near future.
rewritten the file naming, to prevent ending up with 1000 of {randomstring}_chromedriver.exe 's instead it is just called undetected_chromedriver.exe
cleanup removed compat,v2 files and tests folder
added WebElement.click_safe() method, which you can try in case you get detected after clicking a link. This is not guaranteed t o work.
added WebElement.children(self, tag=None, recursive=False) to easily get/find child nodes. example:
body = driver.find_element('tag name', 'body')
# get the 6th child (any tag) of body, and grab all img's within (recursive).
images = body.children()[6].children('img', True)
srcs = list(map(lambda _:_.attrs.get('src'), images))
added example.py where i can point people at when asking silly questions (no, its actually quite cool, everyone should see it)
added support for lambda platform
added support for x86_32
added support for systems reporting as linux2
some refactoring
use_subprocess now defaults to True. too many people don't understand multiprocessing and name == 'main, and after testing, it seems not to make a difference anymore in chrome 104+
added no_sandbox, which defaults to True, and this without the annoying "you are using unsecure command line ..." bar.
update Docker image. you can now vnc or rdp into your container to see the
actual browser window

this version might break your code, test before update!
added new anti-detection logic!
v2 has become the main module, so no need for references to v2 anymore. this mean you can now simply use:
import undetected_chromedriver as uc driver = uc.Chrome() driver.get('https://nowsecure.nl')
for backwards compatibility, v2 is not removed, but aliassed to the main module.
Fixed "welcome screen" nagging on non-windows OS-es. For those nagfetishists who ❤ welcome screens and feeding google with even more data, use Chrome(suppress_welcome=False).
replaced executable_path in constructor in favor of browser_executable_path
which should not be used unless you are the edge case (yep, you are) who can't add your custom chrome installation folder to your PATH
environment variable, or have an army of different browsers/versions and automatic lookup returns the wrong browser
"v1" (?) moved to _compat for now.
fixed dependency versions
ChromeOptions custom handling removed, so it is compatible with webdriver.chromium.options.ChromiumOptions.
removed Chrome.get() fu and restored back to "almost" original:
with statements needed anymore, although it will still work for the sake of backward-compatibility.test success to date: 100%
just to mention it another time, since some people have hard time reading: headless is still WIP. Raising issues is needless
change process creation behavior to be fully detached
changed .get(url) method to always use the contextmanager
changed .get(url) method to use cdp under the hood.
... the with statement is not necessary anymore ..
todo: work towards asyncification and selenium 4
Whenever you encounter the daunted
from session not created: This version of ChromeDriver only supports Chrome version 96 # or what ever version
the solution is simple:
import undetected_chromedriver as uc driver = uc.Chrome( version_main = 95 )
July 2021: Currently busy implementing selenium 4 for undetected-chromedriver
newsflash: https://github.com/ultrafunkamsterdam/undetected-chromedriver/pull/255
To prevent unnecessary hair-pulling and issue-raising, please mind the important note at the end of this document .
<br>Literally, this is all you have to do. Settings are included and your browser executable is found automagically. This is also the snippet i recommend using in case you experience an issue.
import undetected_chromedriver as uc driver = uc.Chrome() driver.get( 'https://nowsecure.nl' ) # my own test test site with max anti-bot protection
Literally, this is all you have to do. If a specified folder does not exist, a NEW profile is created. Data dirs which are specified like this will not be autoremoved on exit.
import undetected_chromedriver as uc options = uc.ChromeOptions() # setting profile options.user_data_dir = "c:\\temp\\profile" # use specific (older) version driver = uc.Chrome( options = options , version_main = 94 ) # version_main allows to specify your chrome version instead of following chrome global version driver.get( 'https://nowsecure.nl' ) # my own test test site with max anti-bot protection
Literally, this is all you have to do. You can now listen and subscribe to the low level devtools-protocol. I just recently found out that is also on planning for future release of the official chromedriver. However i implemented my own for now. Since i needed it myself for investigation.
import undetected_chromedriver as uc from pprint import pformat driver = uc.Chrome(enable_cdp_events=True) def mylousyprintfunction(eventdata): print(pformat(eventdata)) # set the callback to Network.dataReceived to print (yeah not much original) driver.add_cdp_listener("Network.dataReceived", mylousyprintfunction) driver.get('https://nowsecure.nl') # known url using cloudflare's "under attack mode" def mylousyprintfunction(message): print(pformat(message)) # for more inspiration checkout the link below # https://chromedevtools.github.io/devtools-protocol/1-3/Network/ # and of couse 2 lousy examples driver.add_cdp_listener('Network.requestWillBeSent', mylousyprintfunction) driver.add_cdp_listener('Network.dataReceived', mylousyprintfunction) # hint: a wildcard captures all events! # driver.add_cdp_listener('*', mylousyprintfunction) # now all these events will be printed in my console driver.get('https://nowsecure.nl') {'method': 'Network.requestWillBeSent', 'params': {'documentURL': 'https://nowsecure.nl/', 'frameId': 'F42BAE4BDD4E428EE2503CB5A7B4F700', 'hasUserGesture': False, 'initiator': {'type': 'other'}, 'loaderId': '449906A5C736D819123288133F2797E6', 'request': {'headers': {'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT ' '10.0; Win64; x64) ' 'AppleWebKit/537.36 (KHTML, ' 'like Gecko) ' 'Chrome/90.0.4430.212 ' 'Safari/537.36', 'sec-ch-ua': '" Not A;Brand";v="99", ' '"Chromium";v="90", "Google ' 'Chrome";v="90"', 'sec-ch-ua-mobile': '?0'}, 'initialPriority': 'VeryHigh', 'method': 'GET', 'mixedContentType': 'none', 'referrerPolicy': 'strict-origin-when-cross-origin', 'url': 'https://nowsecure.nl/'}, 'requestId': '449906A5C736D819123288133F2797E6', 'timestamp': 190010.996717, 'type': 'Document', 'wallTime': 1621835932.112026}} {'method': 'Network.requestWillBeSentExtraInfo', 'params': {'associatedCookies': [], 'headers': {':authority': 'nowsecure.nl', ':method': 'GET', ':path': '/', ':scheme': 'https', 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'en-US,en;q=0.9', 'sec-ch-ua': '" Not A;Brand";v="99", ' '"Chromium";v="90", "Google ' 'Chrome";v="90"', 'sec-ch-ua-mobile': '?0', 'sec-fetch-dest': 'document', 'sec-fetch-mode': 'navigate', 'sec-fetch-site': 'none', 'sec-fetch-user': '?1', 'upgrade-insecure-requests': '1', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; ' 'x64) AppleWebKit/537.36 (KHTML, like ' 'Gecko) Chrome/90.0.4430.212 ' 'Safari/537.36'}, 'requestId': '449906A5C736D819123288133F2797E6'}} {'method': 'Network.responseReceivedExtraInfo', 'params': {'blockedCookies': [], 'headers': {'alt-svc': 'h3-27=":443"; ma=86400, h3-28=":443"; ' 'ma=86400, h3-29=":443"; ma=86400', 'cache-control': 'private, max-age=0, no-store, ' 'no-cache, must-revalidate, ' 'post-check=0, pre-check=0', 'cf-ray': '65444b779ae6546f-LHR', 'cf-request-id': '0a3e8d7eba0000546ffd3fa000000001', 'content-type': 'text/html; charset=UTF-8', 'date': 'Mon, 24 May 2021 05:58:53 GMT', 'expect-ct': 'max-age=604800, ' 'report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"', 'expires': 'Thu, 01 Jan 1970 00:00:01 GMT', 'nel': '{"report_to":"cf-nel","max_age":604800}', 'permissions-policy': 'accelerometer=(),autoplay=(),camera=(),clipboard-read=(),clipboard-write=(),fullscreen=(),geolocation=(),gyroscope=(),hid=(),interest-cohort=(),magnetometer=(),microphone=(),payment=(),publickey-credentials-get=(),screen-wake-lock=(),serial=(),sync-xhr=(),usb=()', 'report-to': '{"endpoints":[{"url":"https:\\/\\/a.nel.cloudflare.com\\/report?s=CAfobYlmWImQ90e%2B4BFBhpPYL%2FyGyBvkcWAj%2B%2FVOLoEq0NVrD5jU9m5pi%2BKI%2BOAnINLPXOCoX2psLphA5Z38aZzWNr3eW%2BDTIK%2FQidc%3D"}],"group":"cf-nel","max_age":604800}', 'server': 'cloudflare', 'vary': 'Accept-Encoding', 'x-frame-options': 'SAMEORIGIN'}, 'requestId': '449906A5C736D819123288133F2797E6', 'resourceIPAddressSpace': 'Public'}} {'method': 'Network.responseReceived', 'params': {'frameId': 'F42BAE4BDD4E428EE2503CB5A7B4F700', 'loaderId': '449906A5C736D819123288133F2797E6', 'requestId': '449906A5C736D819123288133F2797E6', 'response': {'connectionId': 158, 'connectionReused': False, 'encodedDataLength': 851,


全球首个AI音乐社区
音述AI是全球首个AI音乐社区,致力让每个人都能用音乐表达 自我。音述AI提供零门槛AI创作工具,独创GETI法则帮助用户精准定义音乐风格,AI润色功能支持自动优化作品质感。音述AI支持交流讨论、二次创作与价值变现。针对中文用户的语言习惯与文化背景进行专门优化,支持国风融合、C-pop等本土音乐标签,让技术更好地承载人文表达。


阿里Qoder团队推出的桌面端AI智能体
QoderWork 是阿里推出的本地优先桌面 AI 智能体,适配 macOS14+/Windows10+,以自然语言交互实现文件管理、数据分析、AI 视觉生成、浏览器自动化等办公任务,自主拆解执行复杂工作流,数据本地运行零上传,技能市场可无限扩展,是高效的 Agentic 生产力办公助手。


一站式搞定所有学习需求
不再被海量信息淹没,开始真正理解知识。Lynote 可摘要 YouTube 视频、PDF、文章等内容。即时创建笔记,检测 AI 内容并下载资料,将您的学习效率提升 10 倍。


为AI短剧协作而生
专为AI短剧协作而生的AniShort正式发布,深度重构AI短剧全流程生产模式,整合创意策划、制作执行、实时协作、在线审片、资产复用等全链路功能,独创无限画布、双轨并行工业化工作流与Ani智能体助手,集成多款主流AI大模型,破解素材零散、版本混乱、沟通低效等行业痛点,助力3人团队效率提升800%,打造标准化、可追溯的AI短剧量产体系,是AI短剧团队协同创作、提升制作效率的核心工具。


能听懂你表达的视频模型
Seedance two是基于seedance2.0的中国大模型,支持图像、视频、音频、文本四种模态输入,表达方式更丰富,生成也更可控。


国内直接访问,限时3折
输入简单文字,生成想要的图片,纳米香蕉中文站基于 Google 模型的 AI 图片生成网站,支持文字生图、图生图。官网价格限时3折活动


职场AI,就用扣子
AI办公助手,复杂任务高效处理。办公效率低?扣子空间AI助手支持播客生成、PPT制作、网页开发及报告写作,覆盖科研、商业、舆情等领域的专家Agent 7x24小时响应,生活工作无缝切换,提升50%效率!


多风格AI绘画神器
堆友平台由阿里巴巴设计团队创建,作为一款AI驱动的设计工具,专为设计师提供一站式增长服务。功能覆盖海量3D素材、AI绘画、实时渲染以及专业抠图,显著提升设计品质和效率。平台不仅提供工具,还是一个促进创意交流和个人发展的空间,界面友好,适合所有级别的设计师和创意工作者。


零代码AI应用开发平台
零代码AI应用开发平台,用户只需一句话简单描述需求,AI能自动生成小程序、APP或H5网页应用,无需编写代码。


免费创建高清无水印Sora视频
Vora是一个免费创建高清无水印Sora视频的AI工具
最新AI工具、AI资讯
独家AI资源、AI项目落地

微信扫一扫关注公众号