/// <summary> /// Executes a script in browser context /// </summary> /// <param name="pageFunction">Script to be evaluated in browser context</param> /// <param name="args">Function arguments</param> /// <remarks> /// If the script, returns a Promise, then the method would wait for the promise to resolve and return its value. /// <see cref="JSHandle"/> instances can be passed as arguments /// </remarks> /// <returns>Task which resolves to script return value</returns> public Task <JSHandle> EvaluateFunctionHandleAsync(string pageFunction, params object[] args) { var list = new List <object>(args); list.Insert(0, this); return(ExecutionContext.EvaluateFunctionHandleAsync(pageFunction, list.ToArray())); }
/// <summary> /// Runs <c>element.querySelectorAll</c> within the page. If no elements match the selector, the return value resolve to <see cref="Array.Empty{T}"/>. /// </summary> /// <param name="selector">A selector to query element for</param> /// <returns>Task which resolves to ElementHandles pointing to the frame elements</returns> public async Task <ElementHandle[]> QuerySelectorAllAsync(string selector) { var arrayHandle = await ExecutionContext.EvaluateFunctionHandleAsync( "(element, selector) => element.querySelectorAll(selector)", this, selector); var properties = await arrayHandle.GetPropertiesAsync(); await arrayHandle.DisposeAsync(); return(properties.Values.OfType <ElementHandle>().ToArray()); }
/// <summary> /// The method runs <c>element.querySelector</c> within the page. If no element matches the selector, the return value resolve to <c>null</c>. /// </summary> /// <param name="selector">A selector to query element for</param> /// <returns>Task which resolves to <see cref="ElementHandle"/> pointing to the frame element</returns> public async Task <ElementHandle> QuerySelectorAsync(string selector) { var handle = await ExecutionContext.EvaluateFunctionHandleAsync( "(element, selector) => element.querySelector(selector)", this, selector); if (handle is ElementHandle element) { return(element); } await handle.DisposeAsync(); return(null); }
/// <summary> /// Fetches a single property from the referenced object /// </summary> /// <param name="propertyName">property to get</param> /// <returns>Task of <see cref="JSHandle"/></returns> public async Task <JSHandle> GetPropertyAsync(string propertyName) { var objectHandle = await ExecutionContext.EvaluateFunctionHandleAsync(@"(object, propertyName) => { const result = { __proto__: null}; result[propertyName] = object[propertyName]; return result; }", this, propertyName); var properties = await objectHandle.GetPropertiesAsync(); properties.TryGetValue(propertyName, out var result); await objectHandle.DisposeAsync(); return(result); }
internal async Task <ElementHandle> GetElementAsync(string selector) { var handle = await ExecutionContext.EvaluateFunctionHandleAsync( "(element, selector) => element.querySelector(selector)", this, selector); var element = handle.AsElement(); if (element != null) { return(element); } await handle.Dispose(); return(null); }
/// <summary> /// Evaluates the XPath expression relative to the elementHandle. If there's no such element, the method will resolve to <c>null</c>. /// </summary> /// <param name="expression">Expression to evaluate <see cref="https://developer.mozilla.org/en-US/docs/Web/API/Document/evaluate"/></param> /// <returns>Task which resolves to an array of <see cref="ElementHandle"/></returns> public async Task <ElementHandle[]> XPathAsync(string expression) { var arrayHandle = await ExecutionContext.EvaluateFunctionHandleAsync( @"(element, expression) => { const document = element.ownerDocument || element; const iterator = document.evaluate(expression, element, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE); const array = []; let item; while ((item = iterator.iterateNext())) array.push(item); return array; }", this, expression ); var properties = await arrayHandle.GetPropertiesAsync(); await arrayHandle.DisposeAsync(); return(properties.Values.OfType <ElementHandle>().ToArray()); }
/// <summary> /// A utility function to be used with <see cref="Extensions.EvaluateFunctionAsync{T}(Task{JSHandle}, string, object[])"/> /// </summary> /// <param name="selector">A selector to query element for</param> /// <returns>Task which resolves to a <see cref="JSHandle"/> of <c>document.querySelectorAll</c> result</returns> public Task <JSHandle> QuerySelectorAllHandleAsync(string selector) => ExecutionContext.EvaluateFunctionHandleAsync( "(element, selector) => Array.from(element.querySelectorAll(selector))", this, selector);