Tearsdontfalls Tearsdontfalls - 10 days ago 6
C# Question

GeckoFX: Cast GeckoNode to GeckoHTMLElement

I simply need to do a Click on the first Child of an Element identified by a unique Class.

My Code so far is:

geckoWebBrowser.Document.GetElementsByClassName("button")[0].FirstChild.Click();


The Problem is that GeckoFX 16's
geckoWebBrowser.Document.GetElementsByClassName
returns an Array of the Type
GeckoNode
, which doesnt have the Click Method, so this Code wouldnt work. On the other side, the Method

geckoWebBrowser.Document.GetElementsByName


returns an Array of the Type
GeckoHTMLElement
, which does have a Click Method.

Based on that fact, I tried casting with the
as
Operator, but this always throws an Exception with the message that it cant be casted.
I searched already alot in GeckoFX's Classes(sadly there isnt any documentation), but I dont found anything


Any help will be greatly appreciated.

Tom Tom
Answer

Before casting you need to ensure the node is actually an element, and not, for example, a text node.

GeckoNode node = geckoWebBrowser.Document.GetElementsByClassName("button")[0].FirstChild;
if (NodeType.Element == node.NodeType)
{
  GeckoElement element = (GeckoElement)node;
  element.Click();
}
else
{
 // Even though GetElementByClassName return type could contain non elements, I don't think
 // it ever would in reality.
 Console.WriteLine("First node is a {0} not an element.", node.NodeType);
}