WebBrowser和内嵌网页的交互

来源:互联网 发布:冰川网络上市定位 编辑:程序博客网 时间:2024/05/16 06:23

通过WebBrowser可以和内嵌其中的网页进行交互。现在分享大部分核心代码如下:

#region Html event and set value methods        private void SetValueById(string id, string value)        {            if (tabBrowser.CurrentWebBrowser.Document != null)            {                HtmlElement he = tabBrowser.CurrentWebBrowser.Document.GetElementById(id);                if (he != null)                {                    he.SetAttribute("value", value);                }            }        }        private void SetInputValueByName(string name, string value)        {            HtmlElementCollection hc = tabBrowser.CurrentWebBrowser.Document.GetElementsByTagName("input");            if (hc != null)            {                foreach (HtmlElement he in hc)                {                    if (he.Name == name)                    {                        he.SetAttribute("value", value);                        break;                    }                }            }        }        private void SetSelectedIndexById(string id, string value)        {            if (tabBrowser.CurrentWebBrowser.Document != null)            {                HtmlElement he = tabBrowser.CurrentWebBrowser.Document.GetElementById(id);                if (he != null)                {                    he.SetAttribute("selectedIndex", value);                }            }        }        private void SetSelectedIndexByName(string name, string value)        {            if (tabBrowser.CurrentWebBrowser.Document != null)            {                HtmlElementCollection hc = tabBrowser.CurrentWebBrowser.Document.GetElementsByTagName("select");                if (hc != null)                {                    foreach (HtmlElement he in hc)                    {                        if(he.Name == name)                            he.SetAttribute("selectedIndex", value);                    }                }            }        }        private void SetCheckBoxById(string id, string value)        {            if (tabBrowser.CurrentWebBrowser.Document != null)            {                HtmlElement he = tabBrowser.CurrentWebBrowser.Document.GetElementById(id);                if (he != null)                {                    he.SetAttribute("checked", value);                }            }        }        private void FireEvent(string id, string eventName, params object[] args)        {            if (tabBrowser.CurrentWebBrowser.Document != null)            {                HtmlElement he = tabBrowser.CurrentWebBrowser.Document.GetElementById(id);                if (he != null)                {                    he.InvokeMember(eventName, args);                }            }        }        private void FireChildClickEvent(string parentId, int childIndex)        {            HtmlElement heDiv = tabBrowser.CurrentWebBrowser.Document.GetElementById(parentId);            if (heDiv != null && heDiv.Children.Count > childIndex)            {                heDiv.Children[childIndex].InvokeMember("click", null);            }        }        private void FireScriptOnPage(string functionname)        {            if (tabBrowser.CurrentWebBrowser.Document != null)            {                tabBrowser.CurrentWebBrowser.Document.InvokeScript(functionname);            }        }        private void FireScriptOnPage(string functionname, object[] args)        {            if (tabBrowser.CurrentWebBrowser.Document != null)            {                tabBrowser.CurrentWebBrowser.Document.InvokeScript(functionname, args);                            }        }        private void FireClickEvent(string id)        {            FireEvent(id, "click");        }        private void FocusOnOption(string selectname, string optionvalue)        {            HtmlElementCollection hc = tabBrowser.CurrentWebBrowser.Document.GetElementsByTagName("select");            if (hc != null)            {                foreach (HtmlElement he in hc)                {                    if (he.Name == selectname)                    {                        foreach (HtmlElement c in he.Children)                        {                            if (c.GetAttribute("value") == optionvalue)                            {                                c.SetAttribute("selected", "true");                                return;                            }                        }                    }                }            }        }        private void FireClickEventOnInput(string name)        {            HtmlElementCollection hc = tabBrowser.CurrentWebBrowser.Document.GetElementsByTagName("input");            if (hc != null)            {                foreach (HtmlElement he in hc)                {                    if (he.Name == name)                    {                        he.InvokeMember("click");                        break;                    }                }            }        }        private HtmlElement GetElementByTagName(string tagName, string name)        {            HtmlElementCollection hc = tabBrowser.CurrentWebBrowser.Document.GetElementsByTagName(tagName);            if (hc != null && hc.Count > 0)            {                foreach (HtmlElement he in hc)                {                    if (he.Name == name)                        return he;                }            }            return null;        }        private void SendKeysToElement(string id, string keys)        {            HtmlElement he = tabBrowser.CurrentWebBrowser.Document.GetElementById(id);            if (he != null)            {                he.Focus();                SendKeys.Send(keys);            }        }        #endregion

方法名还是很明了的,有很多是要通过ElementId来操作的,ID可以通过IE Developer Tools或者Firefox的插件获得,手段有很多。

SendKeysToElement可以把键盘按键发送给指定控件,有些按键是特殊按键,可以参见MSDN:
SendKeysToElement("rsid_select_drop_down_input", “Sample Here”);           

FireScriptOnPage用来调用Javascript的方法,有接受参数的重载方法。

加上这篇介绍的控件鼠标位置,可以适用大部分场景。

原创粉丝点击