[fix]: locator.fill() not setting values#1662
Conversation
🦋 Changeset detectedLatest commit: 218a4ec The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
No issues found across 4 files
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Architecture diagram
sequenceDiagram
participant User
participant Locator as SDK: Locator
participant CDP as Browser: CDP Session
participant JS as Browser: JS Execution Context
Note over User,JS: locator.fill(value) Flow
User->>Locator: fill("2026-01-01")
Locator->>Locator: resolveNode() to get objectId
Note over Locator: CHANGED: Bundle script with<br/>bootstrap & global refs
Locator->>CDP: Runtime.callFunctionOn(objectId, bundledScript)
CDP->>JS: Execute bundled JS on element
alt JS Success
JS->>JS: fillElementValue() executes using globals
JS-->>CDP: { result: { value: ... } }
CDP-->>Locator: Response (no exception)
Locator-->>User: void
else NEW: JS Exception (e.g., element disconnected)
JS-->>CDP: { exceptionDetails: { text, exception: { description } } }
CDP-->>Locator: Response with exceptionDetails
Note over Locator: NEW: Unpack exception details
Locator->>Locator: Throw StagehandLocatorError(message, selector)
Locator-->>User: Error: "Error Filling Element..."
end
Note over User,JS: Logic ensures browser-side helpers are available<br/>and runtime errors are surfaced to the SDK user.
Greptile OverviewGreptile SummaryThis PR fixes a critical bug where Key Changes:
The fix follows existing patterns in the codebase for bundling scripts and handling exceptions, making it a clean and well-integrated solution. Confidence Score: 5/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Client as Locator.fill()
participant CDP as Chrome DevTools Protocol
participant Browser as Browser Runtime
participant Element as DOM Element
Client->>Client: Create fillDeclaration with bootstrap
Note over Client: Bundles locatorScriptBootstrap +<br/>fillElementValue via globalRefs
Client->>CDP: Runtime.callFunctionOn(fillDeclaration)
CDP->>Browser: Execute function on element objectId
Browser->>Browser: Load locatorScriptBootstrap
Note over Browser: Initializes __stagehandLocatorScripts<br/>with all helper functions
Browser->>Element: Call fillElementValue(value)
alt Date/Time/Range input type
Element->>Element: prepareElementForTyping()
Element->>Element: Use native value setter
Element->>Element: dispatchInputAndChange()
Element-->>Browser: Return {status: "done"}
else Text/Email/etc input type
Element->>Element: prepareElementForTyping()
Element-->>Browser: Return {status: "needsinput"}
else Exception occurs
Element-->>Browser: Throw exception
end
Browser-->>CDP: Return result with exceptionDetails (if any)
CDP-->>Client: CallFunctionOnResponse
alt exceptionDetails present
Client->>Client: Extract exception message
Client-->>Client: Throw StagehandLocatorError
else status === "done"
Client-->>Client: Return (complete)
else status === "needsinput"
Client->>CDP: Type text via Input.dispatchKeyEvent
CDP->>Element: Simulate keyboard input
Element-->>CDP: Events dispatched
CDP-->>Client: Success
end
|
why
locator().fill()was silently failing on<input>elements that require direct value setting.fill()function (inlocator.ts) the javascript code passed toRuntime.callFunctionOnonly contained thefillElementValue()function body. (seedom/locatorScripts/scripts.tsfor the function definition offillElementValue())fillElementValue()relies on other helpers defined outside of the function body, and therefore outside of the code that was passed toRuntime.callFunctionOnexceptionDetailsfrom the response ofRuntime.callFunctionOnwhat changed
Runtime.callFunctionOnto include the bundled locator globals which were previously missing.fill()actually works on elements that require direct value setting instead of typingRuntime.callFunctionOn, so that we check if there was an exceptionStagehandLocatorErrortest plan
locator-fill.spec.tsto:.fill()correctly sets valuesStagehandLocatorErrorSummary by cubic
Fixes locator.fill so it correctly sets values on inputs that require direct value assignment, and surfaces runtime errors instead of failing silently.
Written for commit 218a4ec. Summary will update on new commits. Review in cubic