/// <summary> /// Finds any elements matching the ElementTagName and the collection of options in searchOptions /// </summary> /// <param name="searchOptions">Contains our search specification</param> /// <returns>List of objects found matching our search options</returns> public HtmlElementList FindElements(HtmlElementSearchOptions searchOptions) { HtmlElementList rtn = new HtmlElementList(); // Search through every known element in this document object foreach (IHTMLElement element in (IHTMLElementCollection)_doc2.body.all) { // If HtmlElementSearchOptions.ElementTagName is not empty or null, it is a required search parameter // Otherwise, any element tag type will be searched on if (searchOptions.ElementTagName.Equals(element.tagName, StringComparison.InvariantCultureIgnoreCase) || String.IsNullOrEmpty(searchOptions.ElementTagName)) { bool IsMatchingElement = true; // Validate all search option attributes foreach (KeyValue option in searchOptions) { // Attempt to get the attribute // not using DLR //dynamic attribute = element.getAttribute(option.Key.ToLower(), 0); object attribute = element.getAttribute(option.Key.ToLower(), 0); // If key value is null, it will match a non-existing attribute if (attribute == null && option.Value == null) { continue; } // When the attribute is not found on the element, it returns null if (attribute != null) { // Invalidate this element as a valid element if the value does not match the search value if (!option.Value.Equals(attribute.ToString(), StringComparison.InvariantCultureIgnoreCase)) { IsMatchingElement = false; break; } continue; } else { IsMatchingElement = false; break; } } // If we have deemed our element as matching, lets add it to the return collection if (IsMatchingElement) { rtn.Add(element); } } } return(rtn); }
/// <summary> /// Returns the first element whose ID matches the ID parameter /// </summary> /// <param name="ID">ID of element to find</param> /// <returns>Html element found. If no elements are found, returns null</returns> public IHTMLElement FindElementByID(String ID) { IHTMLElement rtn = null; // Instantiate our options to search by ID. // In this case, we will not specify ElementTagName so we can match on any element with matching ID HtmlElementSearchOptions options = new HtmlElementSearchOptions(); options.Add(new KeyValue("ID", ID)); // Run the search HtmlElementList foundElements = FindElements(options); // Save the first element found to our return object if (foundElements.Count > 0) { rtn = foundElements[0]; } return(rtn); }