Esempio n. 1
0
        /// <summary>
        /// 获取CSS Selector(当包含上下文的时候调用此方法)
        /// http://stackoverflow.com/questions/3390396/how-to-check-for-undefined-in-javascript
        /// </summary>
        /// <param name="element"></param>
        public string GetContextSelector(mshtml.IHTMLElement context, mshtml.IHTMLElement element)
        {
            SetContextSelector(context);
            var originalId = element.id;

            if (string.IsNullOrEmpty(originalId))
            {
                var id = "__" + IEUtils.IEVariableNameHelper.CreateVariableName();
                element.id = id;
            }
            mshtml.IHTMLWindow2 parentWindow = ((IHTMLDocument2)(element.document)).parentWindow as mshtml.IHTMLWindow2;
            IEUtils.RunScript(ScriptLoader.GetJquerySelector(""), parentWindow);
            StringBuilder sb = new StringBuilder();

            sb.Append(string.Format("var el = $jq('#{0}');", element.id));
            sb.AppendLine();

            sb.Append("if(typeof __context == 'undefined'){");
            sb.AppendLine();
            sb.Append("__context = '';");
            sb.AppendLine();
            sb.Append("}");
            sb.AppendLine();
            sb.Append(" var selector = el.getCssPath(__context);");
            sb.AppendLine();
            sb.Append("__context = 'undefined';");
            sb.Append("return selector");
            object result = JS.FunEval(AppContext.Current.Window, sb.ToString());

            element.id = originalId;
            return(result == null ? "" : result.ToString());
        }
Esempio n. 2
0
        /// <summary>
        /// 还原当前页面SESSION
        /// </summary>
        static public void RestorePageSession(this Browser browser)
        {
            RequireJQueryInstall(browser);
            string fileName = browser.Uri.Host;

            fileName = Path.Combine(Environment.GetFolderPath(
                                        Environment.SpecialFolder.ApplicationData), string.Format("{0}.session", fileName));
            StreamReader sw      = null;
            string       session = "";

            if (!File.Exists(fileName))
            {
                return;
            }
            try
            {
                sw      = new StreamReader(fileName);
                session = sw.ReadToEnd();
            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();
                }
            }

            string code = @"
                    var obj = " + session + @";
                          
                    return obj.url;       
                ";
            object url  = JS.FunEval(browser, code);

            if (url != null)
            {
                //先打开原始页面
                browser.GoTo(url.ToString());
                //回复FORM内容
                code = @"
                    var obj =" + session + @";
                    $jq('form').deserialize(obj.form);                          
                   ";
                IEUtils.RunScript(code, GetWindows(browser));
                browser.AttachToIE();
            }
        }