public async Task <ElementHandle> AdoptElementHandleAsync(ElementHandle handle, FrameExecutionContext to) { var result = await _session.SendAsync(new PageAdoptNodeRequest { FrameId = handle.Context.Frame.Id, ObjectId = handle.RemoteObject.ObjectId, ExecutionContextId = ((FirefoxExecutionContext)to.Delegate).ExecutionContextId, }).ConfigureAwait(false); return(to.CreateHandle(result.RemoteObject) as ElementHandle); }
public async Task <T> EvaluateAsync <T>(FrameExecutionContext context, bool returnByValue, string script, object[] args) { string suffix = $"//# sourceURL={EvaluationScriptUrl}"; RemoteObject remoteObject = null; if (script.IsJavascriptFunction()) { var result = await _client.SendAsync(new RuntimeCallFunctionOnRequest { FunctionDeclaration = $"{script}\n{suffix}\n", ExecutionContextId = _contextId, Arguments = args.Select(a => FormatArgument(a, context)).ToArray(), ReturnByValue = returnByValue, AwaitPromise = true, UserGesture = true, }).ConfigureAwait(false); if (result.ExceptionDetails != null) { throw new PlaywrightSharpException($"Evaluation failed: {GetExceptionMessage(result.ExceptionDetails)}"); } remoteObject = result.Result; } else { string expressionWithSourceUrl = _sourceUrlRegex.IsMatch(script) ? script : script + '\n' + suffix; var result = await _client.SendAsync(new RuntimeEvaluateRequest { Expression = expressionWithSourceUrl, ContextId = _contextId, ReturnByValue = returnByValue, AwaitPromise = true, UserGesture = true, }).ConfigureAwait(false); if (result.ExceptionDetails != null) { throw new PlaywrightSharpException($"Evaluation failed: {GetExceptionMessage(result.ExceptionDetails)}"); } remoteObject = result.Result; } return((T)(returnByValue ? GetValueFromRemoteObject <T>(remoteObject) : context.CreateHandle(remoteObject))); }
private async Task <ElementHandle> AdoptBackendNodeIdAsync(int backendNodeId, FrameExecutionContext to) { DOMResolveNodeResponse result = null; try { result = await Client.SendAsync(new DOMResolveNodeRequest { BackendNodeId = backendNodeId, ExecutionContextId = ((ChromiumExecutionContext)to.Delegate).ContextId, }).ConfigureAwait(false); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); } if (result == null || result.Object.Subtype == "null") { throw new PlaywrightSharpException("Unable to adopt element handle from a different document"); } return(to.CreateHandle(result.Object) as ElementHandle); }