feat: Add page lifecycle hooks to BrowserPool#1791
Merged
vdusek merged 4 commits intoapify:masterfrom Mar 18, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds four page lifecycle hooks to BrowserPool — pre_page_create_hook, post_page_create_hook, pre_page_close_hook, and post_page_close_hook — registered as decorators, aligning Python's BrowserPool closer to the JS Crawlee browser pool API as described in issue #1741.
Changes:
- Four hook registration methods added to
BrowserPool, each appending user-provided async callables to internal lists that are executed at the corresponding lifecycle point. - Close hooks are implemented by monkey-patching
page.closevia_override_page_closeafter page creation. - Comprehensive tests covering individual hook invocation, execution order, argument validation, and multiple hook registration.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/crawlee/browsers/_browser_pool.py |
Adds hook storage lists, _execute_hooks helper, _override_page_close for close hooks, and four public hook registration methods with docstrings. |
tests/unit/browsers/test_browser_pool.py |
Adds six tests covering each hook type, execution order, and multiple hook registration. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Pijukatel
approved these changes
Mar 16, 2026
vdusek
reviewed
Mar 16, 2026
vdusek
reviewed
Mar 16, 2026
Collaborator
vdusek
left a comment
There was a problem hiding this comment.
And also could you please consider whether we should not document this feature somewhere?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Add four page lifecycle hooks to
BrowserPoolregistered as decorators:pre_page_create_hook— called before page creation;browser_new_context_optionsis mutable, so the hook can affect how the page context is configured.post_page_create_hook— called after page creation.pre_page_close_hook— called before page close.post_page_close_hook— called after page close.Issues
Testing
BrowserPool.