scrapy-seleniumbase-cdp
Scrapy downloader middleware that uses SeleniumBase's pure CDP mode to make requests, allowing to bypass most anti-bot protections (e.g. CloudFlare).
Using Selenium's pure CDP mode also makes the middleware more platform independent as no WebDriver is required.
Table of contents
- Installation
- Configuration
- Usage
- Error handling
- Stats
- Enabling debug logs
- Tips for headless Linux environments
- Architecture
- License
Installation
pip install scrapy-seleniumbase-cdp
Configuration
-
Add the
SeleniumBaseAsyncCDPMiddlewareto the downloader middlewares:DOWNLOADER_MIDDLEWARES = { 'scrapy_seleniumbase_cdp.SeleniumBaseAsyncCDPMiddleware': 800 }
-
If needed, configuration can be provided to the SeleniumBase browser instance. For example, to enable the built-in ad blocker (blocks 30+ ad and tracking domains via CDP):
SELENIUMBASE_BROWSER_OPTIONS = { 'ad_block': True, }
Usage
To have SeleniumBase handle requests, use the
scrapy_seleniumbase_cdp.SeleniumBaseRequest instead of Scrapy's built-in
Request:
from scrapy_seleniumbase_cdp import SeleniumBaseRequest
async def start(self):
yield SeleniumBaseRequest(url=url, callback=self.parse_result)
response.meta['cookies'] holds the cookies Chrome would send to the page's final URL, as a list of CDP cookie dicts (name, value, domain, path, secure, httpOnly, expires, …). Scrapy's Request(cookies=...) accepts that list, so a plain request can reuse the browser's session:
def parse_result(self, response):
yield scrapy.Request(next_url, cookies=response.meta['cookies'])
Additional arguments
The scrapy_seleniumbase_cdp.SeleniumBaseRequest accepts additional
arguments. They are executed in the order presented below:
page_load_timeout
Maximum number of seconds to wait for both the page's document response and the page load event before proceeding. If only the load event is missing when the timeout is reached, a warning is logged and the request continues. If the document response is missing, the request fails with TimeoutError (see Error handling). Defaults to 10.
Captcha handling
After navigating to a page, the middleware waits for both the HTTP response status and the page load event. It then attempts to solve any captcha present on the page using SeleniumBase's built-in solver, retrying up to a configurable maximum number of attempts.
The delay before each solve attempt depends on the latest HTTP status code of the page:
- Blocked responses (status in
captcha_blocked_codes): waitcaptcha_blocked_delayseconds (default4) - Any other status: wait
captcha_delayseconds (default0)
So when a click on a captcha reloads the page with a 200, the check that follows waits captcha_delay, not captcha_blocked_delay.
yield SeleniumBaseRequest(
url=url,
callback=self.parse_result,
captcha_delay=1,
captcha_blocked_delay=5,
captcha_blocked_codes=[403, 429, 503],
captcha_max_attempts=5)
Available captcha configuration:
captcha_delay: Seconds to wait before solving when the status is not incaptcha_blocked_codes. Defaults to0.captcha_blocked_delay: Seconds to wait before solving on a blocked response. Defaults to4.captcha_blocked_codes: List of HTTP status codes treated as blocked. Defaults to[403, 429, 503].captcha_max_attempts: Maximum number of solve attempts. Defaults to3. After exhausting all attempts the middleware continues normally but logs a warning. Set it to0to skip captcha solving.captcha_only_when_blocked: Attempt captcha solving only when the page loads with a status incaptcha_blocked_codes. Defaults toFalse, which checks every page, as some sites show a captcha on a200page. Each check reads the whole page and took about 1.5s per page on a Raspberry Pi, so set it toTruewhen the site serves its challenges with a blocked status.
A click on a captcha only starts its verification, so after each click the middleware waits up to page_load_timeout seconds for the page to reload before the next attempt checks whether the captcha is gone.
response.meta['captcha_unsolved'] is True when every attempt still found a captcha, and False otherwise. A challenge page can pass wait_for_element (Cloudflare's has an h1), so check it to tell a blocked page from the real one:
def parse_result(self, response):
if response.meta['captcha_unsolved']:
self.logger.warning(f'blocked by a captcha: {response.url}')
return
wait_for_element / element_timeout
When used, SeleniumBase will wait for the element with the given CSS selector to appear. The default timeout value is of 10 seconds but can be changed if needed. If the element is not found within the timeout, a full-page error screenshot is captured and stored in request.meta['error_screenshot'], then a TimeoutError is raised. The screenshot image format is taken from the request's screenshot configuration if set, otherwise it defaults to PNG.
TimeoutError is an OSError, so Scrapy's RetryMiddleware retries the request (RETRY_TIMES, 2 by default; set meta={'dont_retry': True} to skip that). Once retries run out, the request's errback receives the TimeoutError, and the last attempt's error screenshot is in failure.request.meta['error_screenshot']:
yield SeleniumBaseRequest(
url=url,
callback=self.parse_result,
errback=self.handle_error,
wait_for_element='h1.some-class',
element_timeout=5)
def handle_error(self, failure):
screenshot = failure.request.meta.get('error_screenshot')
if screenshot:
with open('error.png', 'wb') as f:
f.write(screenshot)
browser_callback
If needed, it is possible to provide a callback to interact with the browser
instance and/or its tabs. The return value of the async callback is stored in
response.meta['callback']. Requests always load in the tab the middleware set up when the spider opened, so a tab the callback opens does not take them over.
async def start(self):
async def maximize_window(browser: Browser):
await browser.main_tab.maximize()
yield SeleniumBaseRequest(…, browser_callback=maximize_window)
script
When used, SeleniumBase will execute the provided JavaScript code.
yield SeleniumBaseRequest(
# …
script='window.scrollTo(0, document.body.scrollHeight)')
If the script returns a Promise, it is possible to await its result:
yield SeleniumBaseRequest(
# …
script={
'await_promise': True,
'script': '''
document.getElementById('onetrust-accept-btn-handler').click()
new Promise(resolve => setTimeout(resolve, 1000))
'''
})
The result of the JavaScript code is stored in response.meta['script'].
screenshot
When used, SeleniumBase will take a screenshot of the page and the binary data
will be stored in response.meta['screenshot']:
yield SeleniumBaseRequest(url=url, callback=self.parse_result, screenshot=True)
def parse_result(self, response):
# …
with open('image.png', 'wb') as image_file:
image_file.write(response.meta['screenshot'])
You can also specify additional configuration options:
yield SeleniumBaseRequest(…, screenshot={'format': 'jpg', 'full_page': False})
Or provide a path to automatically save the screenshot (in this case, the image data is not stored in the response):
yield SeleniumBaseRequest(…, screenshot={'path': 'output/image.png'})
Available configuration keys:
path: File path where screenshot will be saved. Useautofor SeleniumBase default path. Leave empty to return data in responsemeta.format: Image format, defaults topng,jpgalso available.full_page: Capture full page or just viewport, defaults toTrue.
Error handling
The middleware checks the HTTP status code right after loading the page to determine captcha-solving behaviour (see Captcha handling above).
The response returned to Scrapy carries the status of the last document response the tab's main frame received, so a redirect gives the target page's status and an iframe's status is ignored. When a solved captcha reloads the page, that is the reloaded page's status (usually 200), not the challenge's 403 or 429, so Scrapy's RetryMiddleware does not retry a page that loaded fine.
- No document response: if the main frame gets no document response within
page_load_timeoutseconds, for example because the server is too slow or the host cannot be resolved, an error is logged andTimeoutErroris raised. Scrapy'sRetryMiddlewareretries the request, then passes theTimeoutErrorto the request'serrback. wait_for_elementtimeout: if the expected element is not found withinelement_timeoutseconds, a full-page error screenshot is captured and stored inrequest.meta['error_screenshot'], thenTimeoutErroris raised. Scrapy'sRetryMiddlewareretries the request, then passes theTimeoutErrorto the request'serrback, where the screenshot is infailure.request.meta['error_screenshot'](seewait_for_elementfor an example).
Stats
The middleware counts captcha outcomes in Scrapy's stats (crawler.stats), so they show up in the stats dump at the end of a crawl and in any project that persists stats, whatever the log level:
captcha/detected: requests whose page showed a captcha on the first solve attempt.captcha/solved: requests where a later attempt found the captcha gone.captcha/unsolved: requests where every attempt still found a captcha (captcha_max_attemptsreached).seleniumbase/response_status_count/<code>: the status the page loaded with, before any captcha was solved. Scrapy's owndownloader/response_status_count/<code>counts the status returned to Scrapy, which is the reloaded page's after a solved captcha. A request that gets no document response is not counted here, as it fails withTimeoutError.
For example, many seleniumbase/response_status_count/403 with no captcha/detected means the site blocks without a captcha the solver recognises, while a high captcha/unsolved means the solver runs but fails.
A failing browser callback, script or screenshot is logged and the response still goes through without its result, so each failure is also counted: seleniumbase/browser_callback_failed, seleniumbase/script_failed, seleniumbase/screenshot_failed, and seleniumbase/error_screenshot_failed for the screenshot taken when wait_for_element times out.
Tips for headless Linux environments
When running Scrapy with this middleware in headless mode using Xvfb on Linux,
you may want to record or visually inspect browser sessions for debugging
purposes. The examples below assume an Xvfb display at :1001 — adjust to
match your setup.
Recording an Xvfb session with ffmpeg
Use ffmpeg to capture the virtual display as a video file:
ffmpeg -f x11grab -r 30 -s 1440x900 -i :1001 \
-codec:v libx264 -preset ultrafast -pix_fmt yuv420p \
/home/user/session_$(date +%Y%m%d_%H%M%S).mp4
Key flags:
-f x11grab— capture from an X11 display-r 30— frame rate (30 fps)-s 1440x900— resolution (must match your Xvfb geometry)-i :1001— X display to capture
Connecting via VNC to an Xvfb session
Use x11vnc to expose the virtual display over VNC for live inspection:
x11vnc -display :1001 -passwd secret -forever -xkb
Then connect from any VNC client to <host>:5900. Key flags:
-display :1001— X display to share-passwd secret— VNC password-forever— keep the server running after the first client disconnects-xkb— use XKEYBOARD extension for better keyboard handling
Enabling debug logs
The middleware logs operational details (page load events, captcha attempts,
screenshot captures, etc.) at the DEBUG level. Log messages are emitted under
the scrapy_seleniumbase_cdp.middleware_async logger name. Warnings and errors
(page load timeouts, element wait timeouts, max captcha attempts reached) use
higher log levels and are always visible.
To see all debug output, set Scrapy's global log level in your settings.py:
LOG_LEVEL = 'DEBUG'
If you prefer to keep Scrapy's own output at a higher level and only enable debug logging for this middleware, configure the parent logger directly:
# settings.py or spider __init__
import logging
logging.getLogger('scrapy_seleniumbase_cdp').setLevel(logging.DEBUG)
This works because the middleware uses a module-level logger named
scrapy_seleniumbase_cdp.middleware_async, which inherits settings from the
scrapy_seleniumbase_cdp parent logger.
You can also use Scrapy's per-module log configuration via the
LOG_CATEGORIES setting (Scrapy ≥ 2.8):
LOG_CATEGORIES = {
'scrapy_seleniumbase_cdp': 'DEBUG',
}
Architecture
See docs/ARCHITECTURE.md for a detailed overview of
the middleware internals, including a sequence diagram of the request processing
pipeline.
License
This project is licensed under the MIT License. It is a fork of Quartz-Core/scrapy-seleniumbase which was originally released under the WTFPL.
Metadata
Release files for scrapy-seleniumbase-cdp 2.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| scrapy_seleniumbase_cdp-2.1.0.tar.gz | 23.2 kB | With provenance | Details for scrapy_seleniumbase_cdp-2.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| scrapy_seleniumbase_cdp-2.1.0-py3-none-any.whl | Python 3 | none | any | Details for scrapy_seleniumbase_cdp-2.1.0-py3-none-any.whl |
Total release size: 37.3 kB
Release files / scrapy_seleniumbase_cdp-2.1.0.tar.gz
| Download URL | scrapy_seleniumbase_cdp-2.1.0.tar.gz |
|---|---|
| Size | 23.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b945bcad7b6a4e8be788832414ffd8f708152f904b48f7918e3324420946c977
|
|
BLAKE2b-256 checksum How to use checksums |
4df32059b2db781fc83c65f435f5ac2dba222ff0570f40738bef9bd9ade67413
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.
Transparency logRelease files / scrapy_seleniumbase_cdp-2.1.0-py3-none-any.whl
| Download URL | scrapy_seleniumbase_cdp-2.1.0-py3-none-any.whl |
|---|---|
| Size | 14.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ade5e9e58c3e6edbfe7b0e927114b874ef6749eaad3faf89c3ef14ab2460fd19
|
|
BLAKE2b-256 checksum How to use checksums |
a1a71fb5e4cbbc3fb937e3a1fd69933cf8a3f3d1bfd77186330bf722540d9ccf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.
Transparency logSecurity for scrapy-seleniumbase-cdp 2.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for scrapy_seleniumbase_cdp-2.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.