コード例 #1
0
        /// <summary>
        /// Changes attribute of given element based on "JavaScriptLocator"
        /// Locator must be unique or first in DOM
        /// </summary>
        /// <param name="element">Any HTML element</param>
        /// <param name="locator">Locator available in JS</param>
        /// <param name="attribute">Name of attribute</param>
        /// <param name="value">Desired value</param>
        /// <returns>Given element</returns>
        public static IWebElement SetAttribute(this IWebElement element, JavaScriptLocator locator, string attribute, string value)
        {
            string js = string.Empty;

            switch (locator)
            {
            case JavaScriptLocator.Id:
                js = string.Format("document.getElementById('{0}').setAttribute('{1}', '{2}')", element.GetAttribute("id"), attribute, value);
                break;

            case JavaScriptLocator.Name:
                js = string.Format("document.getElementsByName('{0}').setAttribute('{1}', '{2}')", element.GetAttribute("name"), attribute, value);
                break;

            case JavaScriptLocator.Tag:
                js = string.Format("document.getElementsByTagName('{0}').setAttribute('{1}', '{2}')", element.GetElementTag(), attribute, value);
                break;
            }

            Browser.JsExecutor.ExecuteScript(js);

            return(element);
        }
コード例 #2
0
        /// <summary>
        /// Changes attribute of given element based on "JavaScriptLocator"
        /// Locator must be unique or first in DOM
        /// </summary>
        /// <param name="jsExe">JavaScript executor for browser</param>
        /// <param name="locator">Locator available in JS</param>
        /// <param name="attribute">Name of attribute</param>
        /// <param name="value">Desired value</param>
        /// <returns>Given element</returns>
        public static IWebElement SetAttribute(this IWebElement element, IJavaScriptExecutor jsExe, JavaScriptLocator locator, string attribute, string value)
        {
            string js = string.Empty;

            switch (locator)
            {
            case JavaScriptLocator.Id:
                js = $"document.getElementById('{element.GetAttribute("id")}').setAttribute('{attribute}', '{value}')";
                break;

            case JavaScriptLocator.Name:
                js = $"document.getElementsByName('{element.GetAttribute("name")}').setAttribute('{attribute}', '{value}')";
                break;

            case JavaScriptLocator.Tag:
                js = $"document.getElementsByTagName('{element.GetElementTag()}').setAttribute('{attribute}', '{value}')";
                break;
            }

            jsExe.ExecuteScript(js);

            return(element);
        }