The isinstance(x, Element) should become isinstance(x, (CachedElement, Element))
isinstance(x, Element)
isinstance(x, (CachedElement, Element))