ExecuteScript() public method

Executes JavaScript in the context of the currently selected frame or window.

The ExecuteScriptmethod executes JavaScript in the context of the currently selected frame or window. This means that "document" will refer to the current document. If the script has a return value, then the following steps will be taken:

For an HTML element, this method returns a IWebElement For a number, a System.Int64 is returned For a boolean, a System.Boolean is returned For all other cases a System.String is returned. For an array,we check the first element, and attempt to return a List{T} of that type, following the rules above. Nested lists are not supported. If the value is null or there is no return value, is returned.

Arguments must be a number (which will be converted to a System.Int64), a System.Boolean, a System.String or a IWebElement. An exception will be thrown if the arguments do not meet these criteria. The arguments will be made available to the JavaScript via the "arguments" magic variable, as if the function were called via "Function.apply"

public ExecuteScript ( string script ) : object
script string The JavaScript code to execute.
return object
            /// <summary>
            /// Method for creating a cookie in the browser.
            /// </summary>
            /// <param name="cookie"><see cref="Cookie"/> that represents a cookie in the browser.</param>
            public void AddCookie(Cookie cookie)
            {
                ////wdAddCookie does not properly add cookies with expiration dates,
                ////thus cookies are not properly deleted. Use JavaScript execution,
                ////just like the Java implementation does.
                ////string cookieString = cookie.ToString();
                ////WebDriverResult result = NativeDriverLibrary.Instance.AddCookie(driver.handle, cookieString);
                ////ResultHandler.VerifyResultCode(result, "Add Cookie");
                StringBuilder sb = new StringBuilder(cookie.Name);

                sb.Append("=");
                sb.Append(cookie.Value);
                sb.Append("; ");
                if (!string.IsNullOrEmpty(cookie.Path))
                {
                    sb.Append("path=");
                    sb.Append(cookie.Path);
                    sb.Append("; ");
                }

                if (!string.IsNullOrEmpty(cookie.Domain))
                {
                    string domain = cookie.Domain;
                    int    colon  = domain.IndexOf(":", StringComparison.OrdinalIgnoreCase);
                    if (colon != -1)
                    {
                        domain = domain.Substring(0, colon);
                    }

                    sb.Append("domain=");
                    sb.Append(domain);
                    sb.Append("; ");
                }

                if (cookie.Expiry != null)
                {
                    sb.Append("expires=");
                    sb.Append(cookie.Expiry.Value.ToUniversalTime().ToString("ddd MM/dd/yyyy HH:mm:ss UTC", CultureInfo.InvariantCulture));
                }

                driver.ExecuteScript("document.cookie = arguments[0]", sb.ToString());
            }
Esempio n. 2
0
        /// <summary>
        /// Compares if two elements are equal.
        /// </summary>
        /// <param name="obj">Object to compare against.</param>
        /// <returns>A boolean if it is equal or not.</returns>
        public override bool Equals(object obj)
        {
            IWebElement other = obj as IWebElement;

            if (other == null)
            {
                return(false);
            }

            if (other is IWrapsElement)
            {
                other = ((IWrapsElement)obj).WrappedElement;
            }

            if (!(other is InternetExplorerWebElement))
            {
                return(false);
            }

            bool result = (bool)driver.ExecuteScript("return arguments[0] === arguments[1];", this, other);

            return(result);
        }