[py] Implement high level APIs for script#17371
Merged
Merged
Conversation
This is finishing the python part of #13992. It adds DOM Mutation and sets the CDP version to be deprecated as part of the longer term move towards Webdriver-Bidi.
Contributor
There was a problem hiding this comment.
Pull request overview
Implements a new high-level Python BiDi Script API for DOM mutation handling, and deprecates legacy CDP-based “pinned script” and log listener APIs in favor of BiDi equivalents.
Changes:
- Add
driver.script.add_dom_mutation_handler()/remove_dom_mutation_handler()(plusDomMutationevent type) via the BiDi codegen enhancements manifest. - Deprecate legacy
RemoteWebDriver.pin_script(),unpin(), andget_pinned_scripts(), and deprecate CDP-based log listeners with guidance toward BiDi Script handlers. - Add test coverage for the new DOM mutation handler and wire the new JS preload helper into Bazel/package data.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| py/test/selenium/webdriver/common/bidi_script_tests.py | Adds tests for BiDi DOM mutation handler registration/removal and multi-handler behavior. |
| py/selenium/webdriver/remote/webdriver.py | Marks script pinning APIs deprecated and emits DeprecationWarning with migration guidance. |
| py/selenium/webdriver/common/log.py | Deprecates CDP-based log/mutation listeners and suppresses internal deprecation warnings from legacy pinning calls. |
| py/private/bidi_enhancements_manifest.py | Adds DomMutation dataclass and mutation handler subscription/unsubscription logic using BiDi script.message + preload scripts. |
| py/BUILD.bazel | Ships bidi-mutation-listener.js as Python package data for the BiDi module. |
| javascript/bidi-support/BUILD.bazel | Exposes bidi-mutation-listener.js to the Python build via Bazel visibility/exports. |
- Cache the mutation preload script ID and add it only once per Script instance to prevent duplicate MutationObservers accumulating on each add_dom_mutation_handler() call - Also invoke the preload script immediately in the current browsing context (call_function) since preload scripts only run on future document creations - Move preload script registration inside the mutation lock to prevent races when two handlers are registered concurrently - Remove find_elements() call from the background event callback (WebDriver is not thread-safe); replace DomMutation.element field with element_id (the raw data-__webdriver_id string) that callers can use to locate the element on the main thread - Relax cross-browser fragile test assertions on old_value/current_value - Replace time.sleep(1) with WebDriverWait until the DOM mutation completes, making the no-callback assertion reliable Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Use a namespaced UUID channel name (selenium.domMutation.<uuid>) instead of the literal 'channel_name' to avoid collisions with user preload scripts or other BiDi channel users - Move add_callback registration to after preload script and session.subscribe setup so a failed setup does not leak a dangling callback - Remove the preload script (remove_preload_script) and clear _mutation_preload_script_id when the last mutation handler is removed, cleaning up the MutationObserver for future navigations - Relax old_value assertion in test: Firefox BiDi returns null for the oldValue of style attribute mutations so skip that check when old_value is None rather than failing the test on Firefox Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
🔗 Related Issues
💥 What does this PR do?
[py] Implement high level APIs for script for bidi and deprecate the CDP versions
🔧 Implementation Notes
🤖 AI assistance
💡 Additional Considerations
🔄 Types of changes