// GetPropertyValue

        private static async Task <string> GetPropertyValueAsync(this ElementHandle handle, string propertyName)
        {
            handle.GuardFromNull();
            var property = await handle.GetPropertyAsync(propertyName).ConfigureAwait(false);

            return(await property.JsonValueAsync <string>().ConfigureAwait(false));
        }
        // ClassList

        /// <summary>
        /// ClassList of the element.
        /// See also https://developer.mozilla.org/en-US/docs/Web/API/Element/classList
        /// </summary>
        /// <param name="handle">An <see cref="ElementHandle"/></param>
        /// <returns>The element <c>classList</c></returns>
        public static async Task <string[]> ClassListAsync(this ElementHandle handle)
        {
            handle.GuardFromNull();
            var json = await handle.EvaluateFunctionWithoutDisposeAsync <JObject>("element => element.classList").ConfigureAwait(false);

            var dictionary = json.ToObject <Dictionary <string, string> >();

            return(dictionary.Values.ToArray());
        }
 /// <summary>
 /// The method runs <c>element.querySelectorAll</c> and then tests a <c>RegExp</c> against the elements <c>textContent</c>. The first element match is returned. If no element matches the selector and regular expression, the return value resolve to <c>null</c>.
 /// </summary>
 /// <param name="elementHandle">An <see cref="ElementHandle"/> to query</param>
 /// <param name="selector">A selector to query element for</param>
 /// <param name="regex">A regular expression to test against <c>element.textContent</c></param>
 /// <param name="flags">A set of flags for the regular expression</param>
 /// <returns>Task which resolves to <see cref="ElementHandle"/> pointing to the element</returns>
 /// <seealso href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp"/>
 public static async Task <ElementHandle> QuerySelectorWithContentAsync(this ElementHandle elementHandle, string selector, string regex, string flags = "")
 {
     return(await elementHandle.GuardFromNull().EvaluateFunctionHandleAsync(
                @"(element, selector, regex, flags) => {
             var elements = element.querySelectorAll(selector);
             return Array.prototype.find.call(elements, function(element) {
                 return RegExp(regex, flags).test(element.textContent);
             });
         }", selector, regex, flags).ConfigureAwait(false) as ElementHandle);
 }
        /// <summary>
        /// The method runs <c>element.querySelectorAll</c> and then tests a <c>RegExp</c> against the elements <c>textContent</c>. All element matches are returned. If no element matches the selector and regular expression, the return value resolve to <see cref="System.Array.Empty{T}"/>.
        /// </summary>
        /// <param name="elementHandle">An <see cref="ElementHandle"/> to query</param>
        /// <param name="selector">A selector to query element for</param>
        /// <param name="regex">A regular expression to test against <c>element.textContent</c></param>
        /// <param name="flags">A set of flags for the regular expression</param>
        /// <returns>Task which resolves to ElementHandles pointing to the elements</returns>
        /// <seealso href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp"/>
        public static async Task <ElementHandle[]> QuerySelectorAllWithContentAsync(this ElementHandle elementHandle, string selector, string regex, string flags = "")
        {
            var arrayHandle = await elementHandle.GuardFromNull().EvaluateFunctionHandleAsync(
                @"(element, selector, regex, flags) => {
                    var elements = element.querySelectorAll(selector);
                    return Array.prototype.filter.call(elements, function(element) {
                        return RegExp(regex, flags).test(element.textContent);
                    });
                }", selector, regex, flags).ConfigureAwait(false);

            if (arrayHandle == null)
            {
                throw new InvalidOperationException("EvaluateFunctionHandleAsync returned null.");
            }

            var properties = await arrayHandle.GetPropertiesAsync().ConfigureAwait(false);

            await arrayHandle.DisposeAsync().ConfigureAwait(false);

            return(properties.Values.OfType <ElementHandle>().ToArray());
        }
예제 #5
0
        /// <summary>
        /// Evaluates the XPath expression relative to the element and returns an <see cref="ElementObject" /> array.
        /// If no elements match the expression, the return value resolve to <see cref="System.Array.Empty{T}"/>.
        /// </summary>
        /// <typeparam name="T">The type of <see cref="ElementObject" /></typeparam>
        /// <param name="elementHandle">A <see cref="ElementHandle" /></param>
        /// <param name="expression">Expression to evaluate <see href="https://developer.mozilla.org/en-US/docs/Web/API/Document/evaluate" /></param>
        /// <returns>Task which resolves to the <see cref="ElementObject" /> array</returns>
        /// <seealso cref="ElementHandle.XPathAsync(string)"/>
        public static async Task <T[]> XPathAsync <T>(this ElementHandle elementHandle, string expression) where T : ElementObject
        {
            var results = await elementHandle.GuardFromNull().XPathAsync(expression).ConfigureAwait(false);

            return(results.Select(x => ProxyFactory.ElementObject <T>(x.GetPage(), x)).ToArray());
        }
예제 #6
0
        /// <summary>
        /// Runs <c>element.querySelector</c> within the element and returns an <see cref="ElementObject" />.
        /// If no elements match the selector, the return value resolve to <c>null</c>.
        /// </summary>
        /// <typeparam name="T">The type of <see cref="ElementObject" /></typeparam>
        /// <param name="elementHandle">A <see cref="ElementHandle" /></param>
        /// <param name="selector">A selector to query element for</param>
        /// <returns>Task which resolves to the <see cref="ElementObject" /></returns>
        /// <seealso cref="ElementHandle.QuerySelectorAsync(string)"/>
        public static async Task <T> QuerySelectorAsync <T>(this ElementHandle elementHandle, string selector) where T : ElementObject
        {
            var result = await elementHandle.GuardFromNull().QuerySelectorAsync(selector).ConfigureAwait(false);

            return(ProxyFactory.ElementObject <T>(elementHandle.GetPage(), result));
        }
        // HasFocus

        /// <summary>
        /// Indicates whether the element has focus or not.
        /// See also https://developer.mozilla.org/en-US/docs/Web/API/DocumentOrShadowRoot/activeElement
        /// </summary>
        /// <param name="handle">An <see cref="ElementHandle"/></param>
        /// <remarks><![CDATA[Elements: <button>, <input>, <keygen>, <select>, <textarea>]]></remarks>
        /// <returns><c>true</c> if the element has focus</returns>
        public static async Task <bool> HasFocusAsync(this ElementHandle handle)
        {
            handle.GuardFromNull();
            return(await handle.EvaluateFunctionWithoutDisposeAsync <bool>("element => element === document.activeElement").ConfigureAwait(false));
        }
        // IsRequired

        /// <summary>
        /// Indicates whether the element is required or not.
        /// </summary>
        /// <param name="handle">An <see cref="ElementHandle"/></param>
        /// <remarks><![CDATA[Elements: <input>, <select>, <textarea>]]></remarks>
        /// <returns><c>true</c> if the element is required</returns>
        public static async Task <bool> IsRequiredAsync(this ElementHandle handle)
        {
            handle.GuardFromNull();
            return(await handle.EvaluateFunctionWithoutDisposeAsync <bool>("element => element.required").ConfigureAwait(false));
        }
        // IsVisible

        /// <summary>
        /// Indicates whether the element is visible or not.
        /// See also https://blog.jquery.com/2009/02/20/jquery-1-3-2-released/#visible-hidden-overhauled
        /// </summary>
        /// <param name="handle">An <see cref="ElementHandle"/></param>
        /// <returns><c>true</c> if the element is visible</returns>
        public static async Task <bool> IsVisibleAsync(this ElementHandle handle)
        {
            handle.GuardFromNull();
            return(await handle.EvaluateFunctionWithoutDisposeAsync <bool>("element => element.offsetWidth > 0 && element.offsetHeight > 0").ConfigureAwait(false));
        }
        // HasClass

        /// <summary>
        /// Indicates whether the element has the specified class or not.
        /// </summary>
        /// <param name="handle">An <see cref="ElementHandle"/></param>
        /// <param name="className">The class name</param>
        /// <returns><c>true</c> if the element has the specified class</returns>
        public static async Task <bool> HasClassAsync(this ElementHandle handle, string className)
        {
            handle.GuardFromNull();
            return(await handle.EvaluateFunctionWithoutDisposeAsync <bool>("(element, className) => element.classList.contains(className)", className).ConfigureAwait(false));
        }
        // ClassName

        /// <summary>
        /// ClassName of the element.
        /// See also https://developer.mozilla.org/en-US/docs/Web/API/Element/className
        /// </summary>
        /// <param name="handle">An <see cref="ElementHandle"/></param>
        /// <returns>The element <c>className</c></returns>
        public static async Task <string> ClassNameAsync(this ElementHandle handle)
        {
            handle.GuardFromNull();
            return(await handle.EvaluateFunctionWithoutDisposeAsync <string>("element => element.className").ConfigureAwait(false));
        }
        // HasContent

        /// <summary>
        /// Indicates whether the element has the specified content or not.
        /// </summary>
        /// <param name="handle">An <see cref="ElementHandle"/></param>
        /// <param name="content">The content</param>
        /// <remarks>Evaluates <c>node.textContent</c></remarks>
        /// <returns><c>true</c> if the element has the specified content</returns>
        public static async Task <bool> HasContentAsync(this ElementHandle handle, string content)
        {
            handle.GuardFromNull();
            return(await handle.EvaluateFunctionWithoutDisposeAsync <bool>("(node, content) => node.textContent.includes(content)", content).ConfigureAwait(false));
        }
        // GetAttribute

        /// <summary>
        /// The value of a specified attribute on the element.
        /// See also https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute
        /// </summary>
        /// <param name="handle">An <see cref="ElementHandle"/></param>
        /// <param name="name">The attribute name</param>
        /// <returns>The attribute value</returns>
        public static async Task <string> GetAttributeAsync(this ElementHandle handle, string name)
        {
            handle.GuardFromNull();
            return(await handle.EvaluateFunctionWithoutDisposeAsync <string>("(element, name) => element.getAttribute(name)", name).ConfigureAwait(false));
        }
 internal static async Task <T> EvaluateFunctionWithoutDisposeAsync <T>(this ElementHandle elementHandle, string pageFunction, params object[] args)
 {
     return(await elementHandle.GuardFromNull().EvaluateFunctionAsync <T>(pageFunction, args).ConfigureAwait(false));
 }