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.