WebBrowser和内嵌网页的交互

本文分享了使用C#中的WebBrowser控件与内嵌网页交互的核心代码片段,包括设置输入框值、触发事件等实用方法,适用于自动化测试或网页表单填写等场景。

通过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的方法,有接受参数的重载方法。

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值