webbrowser 操作网页的几种方法

  public static class WebManager
    {
        /// <summary>
        /// 根据控件ID获取控件对象
        /// </summary>
        /// <param name="web">web对象</param>
        /// <param name="id">控件ID</param>
        /// <returns>对应ID的控件对象</returns>
        private static HtmlElement GetHtmlElementByID(WebBrowser web, string id)
        {
            return web.Document.GetElementById(id);
        }
        /// <summary>
        /// 根据控件标签,和Name获取控件对象
        /// </summary>
        /// <param name="web">web对象</param>
        /// <param name="label">控件标签</param>
        /// <param name="labelName">控件Name</param>
        /// <returns>对应控件标签,及Name的控件对象</returns>
        private static HtmlElement GetHtmlElementByName(WebBrowser web, string label, string labelName)
        {
            var IEnumerator = web.Document.GetElementsByTagName(label).GetElementsByName(labelName).GetEnumerator();
            IEnumerator.MoveNext();
            return (HtmlElement)IEnumerator.Current;
        }
        /// <summary>
        /// 根据控件标签,和控件文本获取控件对象
        /// </summary>
        /// <param name="web"></param>
        /// <param name="label"></param>
        /// <param name="txt"></param>
        /// <returns></returns>
        private static HtmlElement GetHtmlElementByTxt(WebBrowser web, string label, string txt)
        {
            int i = 0;
            HtmlElement element = null;
            var IEnumerator = web.Document.GetElementsByTagName(label).GetEnumerator();
            while (IEnumerator.MoveNext())
            {
                element = (HtmlElement)IEnumerator.Current;
                if (element.OuterText == txt)
                {
                    i = 1;
                    break;
                }
            }
            if (i == 1)
            {
                element.InvokeMember("click");
                return element;
            }
            return element;
        }
        /// <summary>
        /// 点击指定控件标签及标签Name的按钮
        /// </summary>
        /// <param name="web"></param>
        /// <param name="label"></param>
        /// <param name="labelName"></param>
        public static void ClickBtnByName(WebBrowser web, string label, string labelName)
        {
            GetHtmlElementByName(web, label, labelName).InvokeMember("click");
        }
        /// <summary>
        /// 点击指定控件ID的按钮
        /// </summary>
        /// <param name="web"></param>
        /// <param name="id"></param>
        public static void ClickBtnByID(WebBrowser web, string id)
        {
            GetHtmlElementByID(web, id).InvokeMember("click");
        }

        public static bool ClickBtnByTxt(WebBrowser web, string label, string txt)
        {
            HtmlElement btn = GetHtmlElementByTxt(web, label, txt);
            if (btn == null)
            {
                return false;
            }
            btn.InvokeMember("click");
            return true;
        }

        public static void SetTextByName(WebBrowser web, string label, string labelName, string value)
        {
            GetHtmlElementByName(web, label, labelName).SetAttribute("value", value);
        }

        public static void SetTextByID(WebBrowser web, string id, string value)
        {
            GetHtmlElementByID(web, id).SetAttribute("value", value);
        }
        /// <summary>
        /// 获取图像ID
        /// </summary>
        /// <param name="web"></param>
        /// <param name="label"></param>
        /// <param name="id"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static Image GetChangeImgByID(WebBrowser web, string label, string id, int width, int height)
        {
            Image img = null;
            HtmlElement he = GetHtmlElementByID(web, id);
            if (he != null)
            {
                he.Style = "position: absolute; z-index: 9999; top: 0px; left: 0px";
                var b = new Bitmap(width, height);
                web.DrawToBitmap(b, new Rectangle(new Point(), new Size(width, height)));
                img = b;
            }
            return img;
        }
        /// <summary>
        /// 获取图像name
        /// </summary>
        /// <param name="web"></param>
        /// <param name="label"></param>
        /// <param name="name"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static Image GetChangeImgByName(WebBrowser web, string label, string name, int width, int height)
        {
            Image img = null;
            HtmlElement he = GetHtmlElementByName(web, label, name);
            if (he != null)
            {
                he.Style = "position: absolute; z-index: 9999; top: 0px; left: 0px";
                var b = new Bitmap(width, height);
                web.DrawToBitmap(b, new Rectangle(new Point(), new Size(width, height)));
                img = b;
            }
            return img;
        }
        /// <summary>
        /// 获取图像src
        /// </summary>
        /// <param name="web"></param>
        /// <param name="label"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        //public static Image GetChangeImgBySrc(WebBrowser web, string label, string src, int width, int height)
        //{
        //    var va = web.Document.GetElementsByTagName(label).GetEnumerator();
        //    Image img = null;
        //    while (va.MoveNext())
        //    {
        //        HtmlElement he = va.Current as HtmlElement;
        //        if (he.GetAttribute("src").Contains(src))
        //        {
        //            Console.WriteLine(he.ScrollLeft + "-" + he.ScrollTop + "-" + he.OffsetRectangle.X + "-" + he.OffsetRectangle.Y);
        //            he.Style = "position: absolute; z-index: 9999; top: 0px; left: 0px";
        //            web.Select();
        //            var b = new Bitmap(width, height);
        //            web.DrawToBitmap(b, new Rectangle(new Point(), new Size(width, height)));
        //            img = b;
        //            Console.WriteLine(he.ScrollLeft + "-" + he.ScrollTop + "-" + he.OffsetRectangle.X + "-" + he.OffsetRectangle.Y);
        //            break;
        //        }
        //    }
        //    return img;
        //}
        /// <summary>
        /// 获取图像src
        /// </summary>
        /// <param name="web"></param>
        /// <param name="label"></param>
        /// <param name="src"></param>
        /// <returns></returns>
        public static HtmlElement GetChangeImgBySrc(WebBrowser web, string label, string src)
        {
            var va = web.Document.GetElementsByTagName("img").GetEnumerator();
            while (va.MoveNext())
            {
                HtmlElement he = va.Current as HtmlElement;
                if (he.GetAttribute("src").Contains(src))
                {
                    return he;
                }
            }
            return null;
        }
    }

posted @ 2010-12-20 10:59  紫荆飞翔  阅读(819)  评论(0编辑  收藏  举报