Interaction between WebBrowser and embedded web pages

Source: Internet
Author: User
You can use WebBrowser to interact with embedded web pages. Most of the core code is as follows:
        #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
 

The method name is still very clear. There are many operations through ElementId, and IDS can be obtained through IE Developer Tools or Firefox plug-ins. There are many methods.

SendKeysToElement can send the keyboard buttons to the specified control. Some keys are special keys. For details, see MSDN:
SendKeysToElement ("rsid_select_drop_down_input", "Sample Here ");

FireScriptOnPage is used to call Javascript methods. There are overload methods that accept parameters.

The mouse position of the control described in this article can be used in most scenarios.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.