Check if an HTML element is displayed #69

A (X)HTML element is displayed only if its display attribute is not "none" and all of its parent elements are visible.

The above rules need make me think "recursion" so, I developed the following routine to check if a given element in a TWebBrowser control is displayed:

function THTMLDocHelper.ElemIsVisible(const Elem: IDispatch): Boolean;
  Element: IHTMLElement;  // IHTMLElement interface to Elem
  if Supports(Elem, IHTMLElement, Element) then
    // Check if element itself is visible
    Result := ( <> 'none');
    if Result and Assigned(Element.parentElement) then
      // Element is visible: check if parent is visible (recursive)
      Result := ElemIsVisible(Element.parentElement);
    // Not an HTML element
    Result := False;

To use the function pass the IDispatch interface to the required element to it.

Author: Peter Johnson
Contributor: Peter Johnson
Added: 2008/11/26
Last updated: 2008/11/26