Fix ExecuteScript casting exception by converting JsonElement to native types #109
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.
The
ExecuteScriptmethod was throwingInvalidCastException: Object must implement IConvertiblewhen JavaScript returned primitive values like numbers. This occurred because Playwright'sEvaluateAsyncreturnsJsonElementobjects, but Selenium expects native .NET types that implementIConvertible.Problem:
Root Cause:
EvaluateAsyncreturnsJsonElementwithValueKind = Number : "5"JsonElementto native typesJsonElementdoesn't implementIConvertible, causing the exceptionSolution:
Added
JsonElementConverter.ConvertToNativeType()that convertsJsonElementresults to appropriate native .NET types:int,long,double, ordecimal(as appropriate)stringboolnullobject[](with recursive conversion)Dictionary<string, object?>Updated
ExecuteScriptandExecuteAsyncScriptmethods to apply this conversion after getting results from Playwright, ensuring full Selenium compatibility while maintaining backward compatibility.After the fix:
Fixes #108.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.